View Full Version : Using php variable in Javascript?

08-24-2010, 04:33 PM

I am trying to use a php variable in javascript.

At the moment I am using a fixed variable

$articleID = 5;

Then outputting it to the page to declare a javascript variable

var articleid = "<?php echo $articleID; ?>";

Then trying to us it within a string

"FlashVars", "fileURL=../uploadedfiles/+ articleid + .gpx&key=ABQIAAAAOQBC.........

Can anyone tell me what I am doing wrong? I am not very used to Javascript and perhaps I am going about it totally the wrong way.



08-24-2010, 04:40 PM
in contrast to PHP JavaScript doesn’t look for variables in strings.

08-24-2010, 04:43 PM
Does that mean I am going about it totally the wrong way or is it not possible?

08-24-2010, 05:14 PM
you’re doing it wrong. you simple have to construct the result string.

08-24-2010, 05:35 PM
Does that mean what I should be doing is something like this:

var articleid = "fileURL=../uploadedfiles/<?php echo $articleID; ?>.gpx&key=ABQIAAAAOQBC........";

Old Pedant
08-24-2010, 08:06 PM
You could do it that way.

You could also do it the way you started:

articleid = "<?php echo $articleid;?>";
"FlashVars", "fileURL=../uploadedfiles/"+ articleid + ".gpx&key=ABQIAAAAOQBC........."