-1

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.

4
  • 1
    A php array is not a javascript array. You should consider changing your show_array() onclick event to include a json_encode(). Commented Sep 17, 2013 at 23:29
  • I used json_encode when I defined $unmatched. Commented Sep 17, 2013 at 23:32
  • I belive var name1 = val1, name2 = val2 ... ; is correct syntax. stackoverflow.com/questions/694102/… Commented Sep 17, 2013 at 23:56
  • Your first approach (now commented out) of simply echoing out the json_encoded array such as to make a javascript literal is probably the best Commented Sep 18, 2013 at 0:18

3 Answers 3

1

You can try using JSON_NUMERIC_CHECK as the second parameter in json_encode()(Requires PHP >= 5.3.3).

Another option is using parseInt() in JavaScript:

result += parseInt(array[i], 10);
1

This

...onClick=\"show_array(<?php echo $unmatched; ?>);\">" . count_courses($GLOBALS['bulletin_text']) . "</span>" . "</strong></center>";

should be

...onClick='show_array($unmatched);'>" . count_courses($GLOBALS['bulletin_text']) . "</span>" . "</strong></center>";

that is remove the opening an closing php tags and the semicolon, you're already in php parsing mode.

3
  • I tried that, but I still don't get an alert message when I click on "108," meaning show_array() isn't running. Here is the span in the Page Source after this modification: "<span onClick="show_array( ["220","221","242E","249B","250","254","255","256","256S","272A","285"]);">" Commented Sep 17, 2013 at 23:38
  • @GaryGreenhorn notice the single quotes around show_array, if you want to use " then you'll have to html encode the json also. Commented Sep 18, 2013 at 0:32
  • @GaryGreenhorn the " quoting the strings in the array will close the onclick attribute. Commented Sep 18, 2013 at 1:13
0

The html below has two important sections. The first one shows minimal php, and mostly literal markup. The second has a php wrapper around the entire content of the first one.

I replaced some code with literals for simplicity.

<html>
  <head>
    <script>
      function show_array (array) {
        var result = "",
        length = array.length;
        for (var i = 0; i < length; ++i) {
          result += array[i] + "\n";
        }
        alert(result);
      }
    </script>
  </head>
  <body>
    <?php $unmatched = json_encode(array(1,2,3,4,5)); ?>

    <!-- Minimal php, only to pass the php array to JavaScript. -->
    <center><strong>
      Total number of courses parsed: 
      <span onClick="show_array(<?php echo $unmatched ?>)">
        15
      </span>
    </strong></center>

    <!-- Wrapper in php. 
         1. Wrap the previous markup in an echo.
         2. Escape the double quotes.
         3. Remove the php stuff around $unmatched.
         4. Profit.
    -->
    <?php echo "
    <center><strong>
      Total number of courses parsed: 
      <span onClick=\"show_array( $unmatched )\">
        15
      </span>
    </strong></center>
    "; ?>

  </body>
</html>

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.