Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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