PHP generated array then json_encode as output Javascript. I have two php script,
the one with hard coded array works and the other one with mysql generated array does not work. Your help in making the mysql one work will be highly appreciated.
PHP that works, the JSON passed to javascript which displays JSON object correctly.
$arr = array(
array(
"first_name" => "Darian",
"last_name" => "Brown",
"age" => "28",
"email" => "darianbr@example.com"
),
array(
"first_name" => "John",
"last_name" => "Doe",
"age" => "47",
"email" => "john_doe@example.com"
)
);
echo json_encode($arr);
?>
php with mysql generated array NOT work, javascript cannot display the json object.
$result_event = mysqli_query($bd, $qry);
$json = array();
while ($r = mysqli_fetch_assoc($result_event)) {
$json[] = array("first_name" => $r['first_name'], "last_name" => $r['last_name'], "age" => $r['age'], "email" => $r['email']);
}
echo json_encode($json);
mysqli_close($bd);
Here's the Javascript:
<script type='text/javascript'>
$(document).ready(function () {
/* call the php that has the php array which is json_encoded */
$.getJSON('json_encoded_array.php', function(data) {
//$.getJSON('getEventJSON.php', function (data) {
/* data will hold the php array as a javascript object */
$.each(data, function (key, val) {
$('ul').append('<li id="' + key + '">' + val.first_name + ' ' + val.last_name + ' ' + val.email + ' ' + val.age + '</li>');
//$('ul').append('<li id="' + key + '">' + val.active + ' ' + val.event_id + '</li>');
});
});
});
</script>