Re: Use PHP var in PHP generated Javascript?



On Jan 1, 5:36 pm, Bill H <b...@xxxxxxxxx> wrote:
On Jan 1, 6:03 pm, Max <m...@xxxxxxxxxxx> wrote:



I know the common way of using a PHP variable in javascript via:

<script>myjsvar = <?echo $phpvar;?>;</script>

But in my case I'm generating several javascript code blocks (which
produce some flash) via a PHP loop, so I need to figure out another
way to get the PHP vars into these blocks similar to:

foreach ($lines as $line) {

     print "<script>myjsvar = $phpvar;</script>";

}

I've tried various ways to get this working included ".$phpvar." and "+
$phpvar+", etc. None seem to work. There has to be a way to make this
work.

Any suggestions?

Thanks,
Max

Wouldn't

print "<script>myjsvar = {$phpvar};</script>";

Work? (note curly braces)

Bill H

Bill, thanks for the reply. I just tested your suggestion and it did
not work. However, after posting I started doing some more experiments
and found the solution (this always happens). Here it is:

print "<script>var mysymbol='".$symbol."';</script>

This works great!

Max
.



Relevant Pages

  • Re: Use PHP var in PHP generated Javascript?
    ... But in my case I'm generating several javascript code blocks (which ... produce some flash) via a PHP loop, so I need to figure out another ... Bill H ...
    (comp.lang.php)
  • Re: Use PHP var in PHP generated Javascript?
    ... But in my case I'm generating several javascript code blocks (which ... produce some flash) via a PHP loop, so I need to figure out another ... Ah, so your problem was not that $phpvar did not work, but the generated javascript was incorrect. ...
    (comp.lang.php)