I'm trying to pass a PHP array to a button's onClick function as a parameter. A single array index works, however, passing the entire array triggers an error which states that an invalid character has been found. I've tried using json_encode on the array and then parsing it into a javascript variable in the onclick function. It still doesn't work. I get the following error when I click the button: Uncaught SyntaxError: Invalid or unexpected token.
This works fine:
onClick="saveimg('<?php $urls = array(); $urls = $chart->getUrls(); echo $urls[0]; ?>')"
But this doesn't:
onClick="saveimg('<?php $urls = array(); $urls = $chart->getUrls(); echo json_encode($urls); ?>')"
Here is the saveimg function:
function saveimg(urls)
{
var l = JSON.parse(urls);
alert(l[0]);
}