1

I'm having a problem with parsing a 2-dimensional PHP array to Javascript via the json_encode function. This is how I populate my PHP array:

if( $result->num_rows > 0 ) {
    $i = 0;
    while( $row = $result->fetch_assoc() ) {
        $idkaart[$i] = array($row["klant_id"],$row["plaats"],$row["land"],$row["mailing"],$row["korting"],$row["tegoed"],$row["status"],
        $row["startdatum"],$row["laatstonline"],$row["kolom"],$row["rij"],$row["fontsize"],$row["overbodenmail"]);
        //echo $idkaart[$i];
        $i += 1;
    }  
}

After that I use a simple double for loop to display the array on my screen, to see if it populated correctly.
The weird part is that when I try to load it into a Javascript variable via json_encode: var temp = <?php echo json_encode($idkaart[0]); ?>; alert( temp ); it gives me as response 'null'

When I do an echo json_encode in the PHP part of my code it gives me a valid response.

I have already checked character encodings and such.

I do have been coding for several hours, so I may be making a noob mistake here

4
  • Can you show the javascript code you use to read the data? Commented Jan 19, 2015 at 22:29
  • is your json_encode and loop above in the same file? Commented Jan 19, 2015 at 22:32
  • <script > function myFunction() { var temp = <?php echo json_encode($idkaart[0]); ?>; alert( temp ); } </script> That's how I read my data in the Javascript bit, nothing spectacular going on there, I use a button to trigger the function btw. Commented Jan 19, 2015 at 22:38
  • The Javascript part and the PHP part are separate, the idea is to first load up an array and then to dynamically be able to display different data without having to reload the page Commented Jan 19, 2015 at 22:45

2 Answers 2

1

I got it working thanks to a stupid brain fart I had.

Apparently the Javascript wasn't working because it was above the PHP code generating the array. I've mod the Javascript and now it works without a hassle.

Thanks everyone for trying to think of a solution for this stupid mistake!

Sign up to request clarification or add additional context in comments.

Comments

0

2-dimensional array is ok for PHP. You should clarify what's your problem. Is it about PHP's data from MySQL or JS from HTML.

View your html resource to check if the data is there first.

<?php
error_reporting(E_ALL);

$a = array(
    array(1,2,3),
    array(1,2,4)
);
echo "<script>var a=";
echo json_encode($a[0]);
echo ";alert(a);</script>";

2 Comments

When using that bit of code and substituting my own array into it, it works fine. Displays the data exactly as it should. But when trying this in a separate <script> block it fails to work
What's separate <script> block ? Forget about PHP. Copy the code generated by PHP to a HTML file and debug Javascript.

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.