bruggefan Posted May 2, 2011 Share Posted May 2, 2011 hi is there an easy way to get a variable in javascript? I have separate php, tpl and js files. With a onload in my tpl a function is called from my js. Is there a way i can get a variable in my php file from that function? in my php file it's called $path with a path in it to an mp3 file. the javascript function should start a audio stream with that path... thx in advance! Quote Link to comment https://forums.phpfreaks.com/topic/235374-php-variables-in-javascript/ Share on other sites More sharing options...
fugix Posted May 2, 2011 Share Posted May 2, 2011 this should help Quote Link to comment https://forums.phpfreaks.com/topic/235374-php-variables-in-javascript/#findComment-1209561 Share on other sites More sharing options...
The Little Guy Posted May 2, 2011 Share Posted May 2, 2011 echo "<script type='text/javascript'>"; echo "var myJSval = '$myPHPval';"; echo "function myFunction(){alert(myJSval);}"; echo "</script>"; echo "<a href='javascript:myFunction();'>Click Me</a>"; OR echo "<script type='text/javascript'>"; echo "function myFunction(myJSval){alert(myJSval);}"; echo "</script>"; echo "<a href='javascript:myFunction('$myPHPval');'>Click Me</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/235374-php-variables-in-javascript/#findComment-1209562 Share on other sites More sharing options...
bruggefan Posted May 2, 2011 Author Share Posted May 2, 2011 tried the first one but I get errors in my js file if i try to get the php value... Quote Link to comment https://forums.phpfreaks.com/topic/235374-php-variables-in-javascript/#findComment-1209571 Share on other sites More sharing options...
bruggefan Posted May 2, 2011 Author Share Posted May 2, 2011 echo "<script type='text/javascript'>"; echo "var myJSval = '$myPHPval';"; echo "function myFunction(){alert(myJSval);}"; echo "</script>"; echo "<a href='javascript:myFunction();'>Click Me</a>"; OR echo "<script type='text/javascript'>"; echo "function myFunction(myJSval){alert(myJSval);}"; echo "</script>"; echo "<a href='javascript:myFunction('$myPHPval');'>Click Me</a>"; the javascript file is loaded with my pagemeta... and the function is calles by a Onload event in a tpl body... so this looks very difficult... Quote Link to comment https://forums.phpfreaks.com/topic/235374-php-variables-in-javascript/#findComment-1209587 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.