I'm trying to read the response from php function but the response is undefined value. But If I use the chromes inspect element and go in network tab can I read the correct value of item.
Here the code:
Javascript function
function chatHeartbeat(){
$.ajax({
url: 'chat.php?action=chatheartbeat',
type: 'post',
dataType: 'json',
success: function(response) {
$.each(response.items, function(item){
//alert(item.m);
$(".chatboxcontent").append('<div class=chatboxmessage><span class="chatboxmessagefrom">'+item.f+': </span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
});
setTimeout('chatHeartbeat();',3000);
},
error: function(){
$(".chatboxcontent").append('<div class=chatboxmessage><span class="chatboxmessagefrom">ERROR!</span><span class="chatboxmessagecontent">UNKNOWN</span></div>');
alert("failed");
}
});
}
Here the php code:
function chatHeartbeat() {
if ($idchat!=""){
$sql = "select * from chat where idchat=".$idchat." and recd=0 order by id ASC";
$result=$mysqli->query($sql);
if ($result->num_rows>0){
while($row=$result->fetch_array()){
$sqlup="UPDATE chat SET recd = '1' WHERE idchat = ".$row['id'];
if ($mysqli->query($sqlup)=== FALSE){
echo "Error updating record" . $mysqli->error;
}
$item.=<<<EOD
{
"f": "{$row['from']}",
"m": "{$row['message']}"
},
EOD;
}
}
}else{
$item.=$item.=<<<EOD
{
"f": "{$mysqli->connect_errno}",
"m": "{$mysqli->connect_error}"
},
EOD;
}
}
}else{
$item.=$item.=<<<EOD
{
"f": "{$mysqli->connect_errno}",
"m": "{$mysqli->connect_error}"
},
EOD;
}
?>
{
"items": [
<?php echo $item;?>
[
}
<?php
}
?>