cfgcjm Posted June 22, 2008 Share Posted June 22, 2008 I have a PHP variable array that holds info from a database. I need to pass a few variables from the PHP into javascript. Anyone know how to do this? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 22, 2008 Share Posted June 22, 2008 <?php echo "<script type=\"text/javascript\">\n"; echo " var jsVariable = $phpVariable;\n"; echo "</script>\n"; ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted June 22, 2008 Share Posted June 22, 2008 If you're looking for "live" interaction back and forth, not just having php set the vars and that's all you need php for, then look into ajax. Quote Link to comment Share on other sites More sharing options...
marklarah Posted June 22, 2008 Share Posted June 22, 2008 Ajax does seem to be worth a look into, but in the meanwhile: <?php $phpvariable = "foo"; // Or whatever ?> <script type="text/javascript"> var jsvariable = "<?=$phpvariable?>"; </script> Works for me, but you should use Ajax if you intend to use server stored data in a client side application and then reupload it back to the server, you want to be careful about javascript injections in the address bar and such. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.