0

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
}
?>
Fabio
  • 85
  • 1
  • 2
  • 14
  • throw a `debugger` in your `success()`. then `console.log(response)` most likely your response is an object and not an array. if that doesn't get you somewhere copy and paste the console.log response so we can look at it – Seth McClaine May 11 '15 at 18:43
  • And your first section is `JavaScript` not `Java` two completely different languages – Seth McClaine May 11 '15 at 18:44

2 Answers2

0

This might happen if you respond with malformed json format. Try simply putting the data in an array/object and then use json_encode on it:

<?php
$data = [];
$data["f"] = "value1";
$data["m"] = "value2";
echo json_encode($data);
?>
Imashcha
  • 314
  • 1
  • 9
  • I did that but the result doesn't change.... I added the Content-Type header too.... if check the result in "chrome inspect element"-"network tab" I have the values but the textarea is empty.... any idea??? – Fabio May 11 '15 at 20:18
  • Make sure the only thing that is printed in the php page is the json data: no spaces or newlines before or after the output or php tags. – Imashcha May 11 '15 at 20:23
  • In the network tab of chrome inspect element I have this one: "f: "guest" m: "akdkadbaczxvzcvzcv" Why the txtarea is empty? – Fabio May 11 '15 at 20:46
  • In console.log I have this error: "Uncaught TypeError: Cannot read property 'length' of undefined" – Fabio May 11 '15 at 21:08
0

While you're usually fine without it, you can and should set the Content-Type header:

<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data);

NOTE: This is taken from another post (Returning JSON from a PHP Script)

Community
  • 1
  • 1
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
  • `header('Content-type: application/json'); echo json_encode($items);` I have the error again. – Fabio May 11 '15 at 21:10