I have a json_encoded PHP array that I want to pass to Javascript:
$unmatched = json_encode(compareCourseNum($GLOBALS['parsed_courseDetails'], get_course_num_array($GLOBALS['bulletin_text'])));
$GLOBALS['unmatched'] = $unmatched;
print "<center><strong>Total number of courses parsed: $number_of_courses/" . "<span onClick=\"show_array(<?php echo $unmatched; ?>);\">" . count_courses($GLOBALS['bulletin_text']) . "</span>" . "</strong></center>";
Yet when I run the script, what is printed is this:
Total number of courses parsed: 98/);">108
And the Javascript doesn't work either. What should be printed is this:
Total number of courses parsed: 98/108
And the Javascript should work when I click on "108," by showing an alert of the elements of the array.
How can I fix this?
Here is the Javascript:
function show_array (array) {
//var array = <?php echo $unmatched; ?>;
alert();
var result = "",
length = array.length;
for (var i = 0; i < length; ++i) {
result += array[i] + "\n";
}
alert(result);
}
UPDATE: I removed the php tags and semicolon so it is now
"<span onClick="show_array( $unmatched);">"
But show_array is still not running! When I look at the Page Source, I see this:
"<span onClick="show_array( ["220","221","242E","249B","250","254","255","256","256S","272A","285"]);">"
Help please? I know it is not something wrong with the code of show_array, but with the array input, because when I pass a numerical array like [133, 234, 424] it works but not with string ones.
UPDATE2:
Okay, I managed to make the Javascript work by replacing the double quotes in the json_encoded array with single quotes:
$unmatched = str_replace('"', '\'', $unmatched);
But I don't understand why I needed to do that.