Reputation: 1462
In my php code, I have
<?php
$test = json_encode($array);//$array is a valid multidimensional array
?>
I am passing this variable to a javascript function and I am trying to set this variable to javascript.
<script>
var test = "<?php echo $test;?>";
</script>
(To clarify I am using codeigniter framework and for simplicity I did not use how I am sending the variable to the page)
But when I execute the above code, I am getting
Uncaught SyntaxError: Unexpected identifier
I have checked all my syntax.
Thank you in advance.
Upvotes: 1
Views: 310
Reputation: 478
It's not required to wrap the output of json_encode in quotes, otherwise it will be interpreted as a string. At which point you'll need to decode it within JavaScript.
Upvotes: 2
Reputation: 71384
Don't put the decoded json array inside double quotes in the javascript. Change to this.
var test = <?php echo $test;?>;
Upvotes: 6