Last Updated: February 25, 2016
·
6.654K
· teknonono

Access php variable from external js file

First in your test.php file
$phpVar = 'from php';
echo "<script type="text/javascript">var jsVar = " $phpVar ";</script>";
echo "<script type="text/javascript" src="external.js"></script>";
Then use it in .js file
alert( jsVar );

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside