Efficient way to Pass variables from PHP to JavaScript -


this question has answer here:

from time time have pass variables php js script. did this:

var js-variable = "<?php echo $php-variable; ?>"; 

but ugly , can't hide js script in .js file because has parsed php. best solution handle this?

if don't want use php generate javascript (and don't mind call webserver), use ajax fetch data.

if want use php, encode json_encode before outputting.

<script>     var myvar = <?php echo json_encode($myvarvalue); ?>; </script> 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -