Jump to content

passing a variable of a php file to a seprate javascript file


nasi

Recommended Posts

HiI want to pass a variable of a php file to a seprate js file. what I have found so far is to use <?php echo $variable ?>, but it doesn't work when the php file and the js file are separated.any one to help??????

Link to comment
Share on other sites

php works on server side where as js works on client side. once php process and served the page you can not use its variable . I am not sure about your purpose though You can also set the value of parameter of js function from a php fileeg

<?php$stringparam='sometext';$intparam=5;echo "<a onclick='somefunc(\"$stringparam\",$intparam)'>click me</a>";?>
and You can use ajax also to interact with php file after the page has been served.
Link to comment
Share on other sites

If the script really is a separate document, your options are limited. AJAX is one solution, as birbal pointed out, and maybe the best one if you already have AJAX utility built into your script.Another solution is to just give up and embed a very small script in the PHP-generated document. Write the JavaScript data into the global space, and the functions in your external script will be able to access it. A fairly harmless compromise.

Link to comment
Share on other sites

If the script really is a separate document, your options are limited. AJAX is one solution, as birbal pointed out, and maybe the best one if you already have AJAX utility built into your script.Another solution is to just give up and embed a very small script in the PHP-generated document. Write the JavaScript data into the global space, and the functions in your external script will be able to access it. A fairly harmless compromise.
thanks
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...