Jump to content

php variables in javascript


bruggefan

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/235374-php-variables-in-javascript/
Share on other sites

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>";

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.