0

JS & PHP file as follows

console.log(my_val_json);
<script async src="<?php echo get_template_directory_uri(); ?>/js/java.js"></script>
<?php $my_php_val = array( 'banana', 'orange'); ?>
<script type="text/javascript">
  var my_val_json = '<?php echo json_encode($my_php_val); ?>' ;
</script>

Upon refreshing the page multiple times, i get the below error in my console log:

Uncaught ReferenceError: my_val_json is not defined

And sometimes this error also appears on the 1st load. Please advice as i am unable to find any answers online for 2 days now. Thanks!

** NOTE: when i change async to defer, there are no errors (so far) **

14
  • Shouldn't it be var my_val_json = '<?php echo json_encode($my_php_val);?>';? (note the quotes). Commented Jun 16, 2015 at 6:49
  • @D4V1D : Stackoverflow removed it for some reason when i copied and pasted. Commented Jun 16, 2015 at 6:57
  • 2
    <? php - the space in between. is it a typo? Commented Jun 16, 2015 at 6:59
  • 1
    What is java.js used for? Commented Jun 16, 2015 at 7:02
  • 1
    see this [stackoverflow.com/questions/24227763/… Commented Jun 16, 2015 at 7:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.