Sunday 15 April 2012

Pass PHP variable to Javascript at a specific point -


I am passing a PHP variable in JavaScript, but only at a specific point in the code As PHP code continues, variables require me to change I can easily run just JavaScript and run PHP variables when the page is loading, but it does not pass the information that I want. I want Javascript to capture the PHP variable at that time, and then Ignore the subsequent changes.

The actual program is working with several thousand lines of code, so I created a simple page. Around the wrong with this issue, the code below shows what I am trying to do .

  & lt ;? Php $ php_var = 'something'; If (true) {$ php_var = 'new'; Echo & lt; Script type = "text / javascript" & gt; Js_var = "& lt ;? php echo $ php_var ;? & gt;"; Warning (js_var); & Lt; / Script & gt; '; } $ Php_var = 'Later changes'; ? & Gt; & Lt; Script type = "text / javascript" & gt; Var js_var; & Lt; / Script & gt;   

This does not work, however. Js_var is set when inline scripts are run, but it is set to only one string which is Instead of actually evaluating it, instead.

PHP runs first, and whatever JS is sent to the browser. I think you want it:

  echo & lt; Script type = "text / javascript" & gt; Js_var = '.json_encode ($ php_var).'; Warning (js_var); & Lt; / Script & gt; ';    

No comments:

Post a Comment