|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
scope of variable inside js function
I was wondering how I could get a php variable to recognized inside a javascript function.
PHP Var: $address = "Street Address"; <script language=javascript> <!-- function addbookmark(){ bookmark="Listing - "+<?=$address?>+" - CompanyName"; } //--> </script> I am tring to concatinate the javascript var bookmark to equil: "Listing - Street Address - CompanyName" The php var $address does not seem to be read inside javascript. Does anybody know how to get the scope of a php variable to work inside this javascript function. Thanks in advance for any help... |
|
#2
|
|||
|
|||
|
This is really a basic php question, and has nothing to do with javascript. You're php expressoin evaluates the address variable, but does not print it out to the resulting html. Look at the resulting source of the function, and you'll see something like this:
Code:
bookmark="Listing - "++" - Company Name"; This obviously won't work. You need to echo/print the value of your php variable into the script. Code:
bookmark = "Listing - " + <? echo $address; ?> + " - Company Name"; |
|
#3
|
||||
|
||||
|
<?=$var?> and <?php echo $var; ?> and <? echo $var; ?> are the same thing. <?= is basically a show way to say 'print this'.
The thing is, php will get parsed before the JS, you need to NOT escape the string in javascript, just put the PHP into it. The way it is now, you'd end up with something like this: Code:
bookmark="Listing - "+1234 fake street. Unknown, NW, 55555+" - Company Name"; or what ever happens to be in the $address variable. That is going to produce a JS error obviosuly. What you need to do is just throw the PHP in to the string directly. Code:
bookmark="Listing - <?=$address?> - Company name"; |
|
#4
|
|||
|
|||
|
Of course, server side and client side. I don't know how I got hung up on that one. Works fine now and makes perfect sence, thanks you for the help.... :-)
|
|
#5
|
|||
|
|||
|
Crap, I didn't catch the "=" in the php. And my solution wouldn't have worked anyway, since I terminated the strings for some gawd awful reason.
My mind is slippin now... |
![]() |
| Viewing: Dev Shed Forums > Web Design > HTML Programming > scope of variable inside js function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|