1

I have a ajax that needs to return some values, but if I use a while() to get all the result from my db, the return is "nothing". Where am I making mistake?

My Ajax script is posted below:

<script type="text/javascript" charset="utf-8">
  function addmsg(type, msg) {
    var obj = jQuery.parseJSON(msg);
    // Your count variable
    var count = obj.count;
    // Your not variable
    var not = obj.not;
    $('#msg_count').html(count);
    $('#notification').html(not);

  }

  function waitForMsg() {
    $.ajax({
      type: "GET",
      url: "notification/select.php",
      cache: false,
      timeout: 50000,

      success: function(data) {
        addmsg("new", data);
        setTimeout(
          waitForMsg,
          1000
        );
      },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
        addmsg("error", textStatus + " (" + errorThrown + ")");
        setTimeout(
          waitForMsg,
          15000);
      }
    });
  };

  $(document).ready(function() {

    waitForMsg();

  });
</script>

My php script is posted below:

 $result = mysqli_query($con, "SELECT * from notification where tousername='$tousername' and isread = 0");

 while ($row = mysqli_fetch_array($result)) {
     $count = $result - > num_rows;
     $not = $row['notification_msg'];
     $res = [];
     $res['count'] = $count;
     $res['not'] = $not;
 }
 echo json_encode($res);
2
  • Does it literally return "nothing" (<-- this is a string), is the server not sending a response or does the server return nothing (<-- this is not a string)? Commented Feb 5, 2016 at 8:19
  • Its not returning anything. No errors too Commented Feb 5, 2016 at 8:29

3 Answers 3

4

You are overwriting your results variable in the loop:

while (...) {
  ...
  $res=[];
  ...
}

You probably want something like:

$res['count'] = $result->num_rows;
while($row = mysqli_fetch_array($result)) {
  $res[]['not'] = $row['notification_msg'];
}
echo json_encode($res);
Sign up to request clarification or add additional context in comments.

8 Comments

Its not returning anything.
@SanjuMenon Where does $tousername come from, do you get any results when you open notification/select.php directly in the browser?
it comes from the database. and iam comparing its with the logged in user.
@SanjuMenon You should add that code and tell us what happens when you open notification/select.php in the browser.
Yes iam getting the proper output in select.php. This is the result...{"count":3,"0":{"not":"New Intender details entered 1"},"1":{"not":"New Intender details entered 2"},"2":{"not":"New Intender details entered 3"}}
|
0

Overwrite your value with same index. you can write this.

while($row = mysqli_fetch_array($result)) {
$count = $result->num_rows;
$not=$row['notification_msg'];
$res=[];
$res['count'][] = $count;
$res['not'][] = $not; 
}

Or

while($row = mysqli_fetch_array($result)) {
$count = $result->num_rows;
$not=$row['notification_msg'];
$res=[];
$res[]['count'] = $count;
$res[]['not'] = $not; 
}

echo json_encode($res);

die();

Write your js addmsg() function.

function addmsg(type, msg) {
    var obj = jQuery.parseJSON(msg);
    // Your count variable
    for(prop in obj){
      var count = obj[prop][count];
      var not = obj[prop][not];
       $('#msg_count').html(count);
       $('#notification').html(not);
    }
  }

4 Comments

Do i need to change anything in the ajax script? like for example..adding an each..
iam sorry, its not returning anything. just blank. just seeing if i have made mistake
Basically iam doing a notification system like fb does. Its shows the count of unread messages. If we click the count, its shows the unread messages
Write die() function end of your echo json_encode($res);
-1

re-write your script

function waitForMsg(){
$.get("notification/select.php",function(callback){
console.log(callback); // here is your data in callback variabel you can in in console log
})
}

Replce this code with your php script.

 $result = mysqli_query($con, "SELECT * from notification where tousername='$tousername' and isread = 0");

       $res['count'] = $result->num_rows;
    while($row = mysqli_fetch_array($result)) {
      $res[]['not'] = $row['notification_msg'];
    }
    echo json_encode($res);

5 Comments

And what about my ajax script? Do i need to change anything?
Yes url is fine. But do i need to add $.each in the ajax to accomodate the while loop?
As of now my ajax script is written not to handle a loop. Am i correct?Am very new to ajax.
yes your script is ok.. but you can use $.get("notification/select.php",function(callback){ "you can looping callback variable here in javascript"})
Did you just literally copy my answer but without the explanation?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.