I'm new to php, but can anyone give me the proper syntax to initialize a javascript variable with a php variable value?
Syntax For Using Php Value In Javascriptphp and javascript
Page 1 of 1
7 Replies - 18230 Views - Last Post: 09 December 2003 - 08:06 PM
Replies To: Syntax For Using Php Value In Javascript
#2
Re: Syntax For Using Php Value In Javascript
Posted 08 December 2003 - 04:25 PM
What is in your javascript that you need? What variable? How is it being passed, what was it from originally?
If you can answer these I can probably give you the best way to do this.
If you can answer these I can probably give you the best way to do this.
#3
Re: Syntax For Using Php Value In Javascript
Posted 08 December 2003 - 04:41 PM
graeder, on Dec 8 2003, 03:59 PM, said:
I'm new to php, but can anyone give me the proper syntax to initialize a javascript variable with a php variable value?
Hmm, I'm not sure about proper syntax, but here's the way I do it (and the way I do it in ASP).
<? $vartopass = "Hello"; ?> <script = "javascript"> var strnewVar = '<?=$vartopass?>'; alert(strnewVar); </script>
This may not be the proper way, but it works for me.
HTH!
#4
Re: Syntax For Using Php Value In Javascript
Posted 08 December 2003 - 05:28 PM
Posted then realized I misread...
This post has been edited by gneato: 08 December 2003 - 05:30 PM
#5
Re: Syntax For Using Php Value In Javascript
Posted 08 December 2003 - 07:15 PM
Amadeus, on Dec 8 2003, 04:41 PM, said:
This may not be the proper way, but it works for me.
That's the ASP way. In PHP, like ASP, you can print any variables to output, including HTML output, including inside a Javascript tag like this:
<?PHP print "<script language=\"Javascript1.2\">\nfoo=$foo;\n</script>"; ?>
(results at http://www.peakepro....h/foo.php?foo=3 )
or this:
<script language="Javascript1.2"> foo=<?PHP print $foo; ?>; </script>
(results at http://www.peakepro..../foo2.php?foo=3 )
Either will work. Be wary of mixing HTML and PHP in large applications; it can create maintenance headaches.
Cheers,
RP
#6
Re: Syntax For Using Php Value In Javascript
Posted 09 December 2003 - 12:26 AM
cyberscribe, on Dec 8 2003, 07:15 PM, said:
<?PHP print $foo; ?>
<?php print $foo; ?> == <?=$foo?>
in more recent versions of PHP...
This post has been edited by gneato: 09 December 2003 - 12:27 AM
#7
Re: Syntax For Using Php Value In Javascript
Posted 09 December 2003 - 06:53 PM
cyberscribe, on Dec 8 2003, 09:15 PM, said:
Either will work. Be wary of mixing HTML and PHP in large applications; it can create maintenance headaches.
Cheers,
RP
Cheers,
RP
As will the method I described. I'll agree with cyberscribe's caveat concerning the mixing of both, however. It can get nasty to debug on large apps.
#8
Re: Syntax For Using Php Value In Javascript
Posted 09 December 2003 - 08:06 PM
gneato, on Dec 9 2003, 12:26 AM, said:
<?php print $foo; ?> == <?=$foo?>
in more recent versions of PHP...
in more recent versions of PHP...
Yikes! PHP is turning into Perl.
Couldn't find the versioning, but found this along the way which is worth noting:
http://www.faqts.com...ml/aid/1/fid/40
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote





|