Reputation: 235
I'm trying to pass a PHP variable to Javascript like this..
var url = <?php echo $urlArray[0]; ?>;
The contents of $urlArray[0] is a string from a json decoded array
"baby"
When I run the code I get the error..
Uncaught ReferenceError: baby is not defined
Upvotes: 0
Views: 117
Reputation: 10564
var url = "<?php echo $urlArray[0]; ?>";
you forgot the quotes.
If you need to export more complicated data structure, you may need json_encode. This might be helpful if exporting arrays and/or objects.
Upvotes: 3
Reputation: 17598
json_encode
is your friend - use to wrap anything you're trying to pass to javascript.
http://php.net/manual/en/function.json-encode.php
var url = <?php echo json_encode($urlArray[0]); ?>;
Upvotes: 2