I have the following PHP:
<?php
$get_messages = mysqli_query($mysqli, "SELECT * FROM messages");
$messages = array();
while ( $row = mysqli_fetch_array($get_messages)){
$messages[] = array( 'name' => $row['name'], 'message' => $row['message']);
}
$json = json_encode($messages);
echo $json;
?>
Which successfully echo the following JSON:
[{"name":"Ernest","message":"hello"},{"name":"Ernest","message":"hello"},{"name":"john","message":"yes"},{"name":"Diana","message":"I know
"},{"name":"fgafg","message":"fgehfegfvg"},{"name":"fgafg","message":"fgehfegfvg"},{"name":"fgafg","message":"fgehfegfvg"},{"name":"gsfvgx","message":"fbhfxvcz"},{"name":"argttrhjyrbsgdzdc","message":"ahjtgfrdcvdfsevb"}]
Then I have this JS:
$(document).ready(function(){
$.ajax({
url: 'retrieve.php',
dataType: 'json',
success: function (response) {
alert(response[3]);
}
});
});
which alerts undefined.
What is the issue here ? Your help will be appreciated.
NOTE: If I change the alert to alert(response[3].name);
or alert(response[3][name]);
the alert window does not even show up to the party