faqts : Computers : Programming : Languages : PHP : Common Problems : Variables

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

131 of 163 people (80%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How to change php variables to javascript variables?

Dec 15th, 2002 02:42
Dan Stockton, Mark O\'Malley,


I'm not 100% if this is what you're looking for! But this seems to allow
php variables to be assigned to javascript variables. Handy for
validation and the like.
<?
# php variable to javascript test
#$test = "hello"; # php variable
#echo "<script> var jstest = '$test'; </script>"; # Assign it to a
javascript variable
#echo "<script>document.write(jstest); </script>"; # print javascript
variable.
?>
_________________________________________________________
.. just another way to do the same thing-
<?$test='hello'?>
<script language="javascript" type="text/javascript">
var jstest='<?=$test?>';
document.write('jstest');
</script>
(maybe it's just coding preference but this one's compliant and 
quicker:)