0

I want to set background color based on user preference that's in a database.

I set a variable named $bc from the session variable I saved from the row.

If I echo the either the session variable ($_SESSION['backcolr']) or the new variable ($bc), the correct value of yellow appears. However, if I try to use either variable with jQuery, it doesn't work. Not sure what I'm doing wrong.

<script type="text/javascript">
    //  $('#body').css(background-color','yellow')  -- this works
    $("body").css("background-color",'$bc');     --- this doesn't work
</script>
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
Deb
  • 1
  • 1

2 Answers2

1

Hello you should modify your code like below;

<script type="text/javascript">
    //  $('#body').css(background-color','yellow')  -- this works
    $("body").css("background-color",'<?PHP echo $bc; ?>');
</script>
Cihan Uygun
  • 2,128
  • 1
  • 16
  • 26
1

Thats because you need to tell PHP when it should be parsing PHP code. Replace that line with:

$("body").css("background-color",'<?php echo $bc; ?>');
Tim Hysniu
  • 1,446
  • 13
  • 24