0

I have got the most biggest problem, I have see.

piece of code (just and example, the main example use database querys):

<?php $var1="999"?> <script> bigvar= <?php echo json_encode($var1); ?>; var lolo = { big: 2 } lolo.big=bigvar; alert(lolo.big); </script> 

Problem: It does not recognize the PHP variable (it doesn't change to 999 value), and passing php value to javascript variable, doesn't work.How can help me ?.It is a big issue.

3
  • What do you mean "does not recognize", PHP is executed before JS, so JS isn't at any time aware that it's PHP providing the value. Commented Oct 1, 2015 at 6:48
  • remove s tag </scripts> Commented Oct 1, 2015 at 6:50
  • if i copy and paste your code i get 999... does it return 2 to you? Commented Oct 1, 2015 at 7:53

3 Answers 3

4

you should add quotes while assigning value from php variable to javascript, like as follows

 <script> bigvar= "<?php echo json_encode($var1); ?>" var lolo = { big: 2 } lolo.big=bigvar; alert(lolo.big); </script> 

and its </script>, not </scripts>

Sign up to request clarification or add additional context in comments.

Comments

1
<script> var bigvar= "<?php echo($var1); ?>" var lolo = { "big": "2" } lolo.big=bigvar; alert(lolo.big); </scripts> 

The above example works fine for me..Try it

1 Comment

Thanks, yes it works, but the main problem (the most bizarre I have see), remains.It is with howler js. Thanks.
0

You have to add quotes around your php, so your JS know that this value is a string.

bigvar = "<?php echo json_encode($var1); ?>"; 

2 Comments

Hi, thanks, I have done it: var bigvar= "<?php echo($var1); ?>" (I just have forgotten, to put ; in my example.Thanks
unfortunately, the issue remains

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.