Advertisement
rucinski69

Set JavaScript variable from PHP

Jun 25th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Set JavaScript variable from PHP
  2. http://stackoverflow.com/questions/18189198/ddg#18189336
  3.  
  4. If you want to set the variable when the page loads, you could use something like this in the PHP code:
  5.  
  6. <script type="text/javascript">var strUser = <?php echo json_encode($someVariable); ?>;</script>
  7.  
  8. Just make sure to remove the later variable declaration from the JavaScript.
  9.  
  10. If you want to set the variable after the page loads, you'll have to use an AJAX call to ge the value from the server.
  11.  
  12. --Joshua Dwire
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement