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
<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.