Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass JSON object from php to javascript in the same script

This is probably an easy question, but I haven't been able to find a complete and specific answer. I create a json object in php with json_encode(), now i just need to get that object in javascript and parse it out. I wanted to do it in the same script, but I can do it another way if need be.

How do i get at this object from javascript?

like image 418
BDUB Avatar asked Mar 08 '26 12:03

BDUB


1 Answers

<?php

$stuff = array('a' => 1, 'b' => 2);

?>

<script type="text/javascript">
  var stuff = <?php print json_encode($stuff); ?>;
  alert(stuff.a); // 1
</script>
like image 139
aw crud Avatar answered Mar 10 '26 02:03

aw crud