Efficient way to Pass variables from PHP to JavaScript -
this question has answer here:
- how pass variables , data php javascript? 17 answers
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
Post a Comment