Attn: JavaScript + PHP
- Started
- Last post
- 8 Responses
- jgjh151
Need to get a php variable into a javascript.
The php var is defined at top of page.
I'm ending up with a javascript error message stating my php variable is undefined in the javascript. I know it is defined outsid of the js. I can echo it, etc.
Here is what I have:
$select is my php variable.
Maybe I can get the $select from the URL into the javascript?Anyone?
- jgjh1510
OK, it took out my whole example. So I try yo post without script tags?
javascript start tag here
var PhpVar = $select;
if (PhpVar == "Hey Now"){
alert("Hey Now!");
}
javascript end tag here
- protoculture0
Not sure if this is it, but don't forget to enclose your $select variable in quotes when assigning it in the javascript. Like this:
var PhpVar = "$select";
- jgjh1510
That makes it a text string.
- jgjh1510
got it
- protoculture0
isn't a text string what your looking for? at least that's what your example looks like.
- ********0
you got it?
- hulja0
if your JavaScript is in the html code, you need PHP tags to let the server know that's a PHP variable you're calling.
I dunno if it'll show, but it should like like this:and if it doesn't show:
[?=$nameofvariable; ?]where the [ and ] are opening and closing bracettes.
- chl0
huija is correct above. email me directly if you have more troubles.