-1

My friend made a Javascript file that projects a graph of random values it generates from 1-20.

var value1 =  (Math.random() * 14).toFixed(0);

I made a PHP file that reads values from an SQL database and assigns that value to $number variable.

How can I assign variable value1 the value of $number. I tried using: var value1 = <?php echo json_encode($number); ?>; but it's not working for me.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
Talha Munir
  • 498
  • 1
  • 4
  • 16
  • 3
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – SuperDJ Apr 05 '18 at 08:29
  • 1
    If the type of `$number` is number then you don't need to call `json_encode()`. If it is not number then `json_encode()` helps you produce correct JavaScript (without syntax errors) but `value1` becomes a string and adding it with another value (string or number) produces unexpected results. – axiac Apr 05 '18 at 09:14
  • *"but it's not working for me."* -- this is definitely not helpful. Describe the actual outcome and the expected outcome. Post the relevant code. – axiac Apr 05 '18 at 09:16

1 Answers1

1

Simply include var value1 = <?php echo $number; ?>; in your script.

Sam Driver
  • 92
  • 1
  • 1
  • 14