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?

87 of 161 people (54%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

How to pass the PHP variable to Javascript?

Sep 18th, 2002 10:33
Jeffrey Lake, Mohammed Ismail,


I came accross this problem recently, and I'm pretty sure there is a 
proper answer out there somewhere.  While you waiting, this may 
interest you:

<?php
 ...[somehow your php generates a $php_variable here]
 print "

   <SCRIPT LANGUAGE=Javascript>
      var javascript_variable = $php_variable;
 ";
...
?>
 ...[rest of Javascript stuff as normal]
</SCRIPT>

Yes, I've actually printed the Javascript code as if it were HTML, and 
yes it does actually run exactly as if you had hardcoded the variable 
into the Javascript.