0

I am sent many dynamic post ids from a page and a php server side page(server.php) make a query with those id to find out newly added data in mysql.

If it not found any newly added data in mysql, it's return a undefined value. So as per my script, It's append a undefined one after one at a time interval.

How to stop this undefined value?

my javascript:

var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i){
CID[i] = $(this).data('post-id');
});

function addrep(type, msg){
CID.forEach(function(id){
    $("#newreply"+id).append("<div class='"+ type +""+ msg.id +"'><ul><div class='cdomment_text'>"+ msg.detail +"</ul></div>");
});
}

function waitForRep(){
    $.ajax({
        type: "GET",
        url: "server.php",
        cache: false,
        contentType: "application/json; charset=utf-8",
        data: {
        // this way array containing all ID's can be sent:
        CID : CID
    },
        timeout:15000, 
        success: function(data){ 
            addrep("postreply", data);
            setTimeout(
                waitForRep, 
                15000 
            );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(
                waitForRep, 
                15000); 
        }
    });
}

$(document).ready(function(){
    waitForRep();
});

server.php

while (true) {
    if($_REQUEST['CID']){  //cid got all dynamic post id as: 1,2,3,4 etc.
      foreach($_REQUEST['CID'] as $key => $value){

        $datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
        $res = mysqli_query($dbh,"SELECT * FROM reply WHERE qazi_id=".$_REQUEST['tutid']."  AND date >= '$datetime' ORDER BY id DESC LIMIT 1") or die(mysqli_error($dbh));
    $data = array();
        while($rows =  mysqli_fetch_assoc($res)){

          $data[]=$rows;

          $data['id'] = $rows['id']; 
          $data['qazi_id'] = $rows['qazi_id'];
          $data['username'] = $rows['username'];
          $data['description'] = $rows['description'];
          $data['date'] = $rows['date'];
          //etc. all
             $id = $rows['id'];
             $qazi_id = $rows['qazi_id'];
             $username = $rows['username'];
             $description = $rows['description'];
             //etc. all
          } //foreach close
      } //foreach close

          if ($description=="") {$detail .= '';}
            else {$detail .=''.$description.'';}
          $data['detail'] = $detail;
          // do somethig

           if (!empty($data)) {
              echo json_encode($data);
              flush();
              exit(0);
           }

    } //request close
    sleep(5);
} //while close
6
  • 1
    Why not just check if the server returns undefined and ignore it if it does? Commented Mar 12, 2015 at 13:25
  • I thought about this but I have no idea about this method. Have you any detail solved. Thank u. Commented Mar 12, 2015 at 13:28
  • PHP does not parse JSON requests sent to it (unless support has been added recently). Remove the contentType option from your ajax request. If you are wanting to accept JSON as a response, set the dataType option to "json" Commented Mar 12, 2015 at 13:31
  • @ Patrick Evans Thank you. Commented Mar 12, 2015 at 13:38
  • .append("+ msg.detail +"); <-- makes no sense Commented Mar 12, 2015 at 13:39

1 Answer 1

1

Edit your addrep function to not insert if it's undefined:

function addrep(type, msg){
CID.forEach(function(id){
    if(msg.detail != "undefined") {
       $("#newreply"+id).append("+ msg.detail +");
    }
});
}

Or just break it if undefined:

function addrep(type, msg){
    CID.forEach(function(id){
        if(msg.detail == "undefined")
           break;
        $("#newreply"+id).append("+ msg.detail +");
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank u. 1st one work as same. and 2nd one stoped working full script.
I want to say problem is not solved, It's work as same before. Thank u.
Try to alert(msg.detail). Replace "undefined" with alerted string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.