2

I need to display the JSON data in my PHP. The data is coming as a loop. The raw data looks like this :

[{
    "count": 3,
    "not": "New Intender details entered"
},{
    "count": 3,
    "not": "New Intender details entered"
},{
    "count": 3,
    "not": "New Intender details entered"
}]

My AJAX script is as follows:

function addmsg(type, msg) {
    var obj = jQuery.parseJSON(msg);
    $.each(obj, function(index, value) {
        var count = value.count;
        var not = value.not;
        alert(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();
});

My HTML is as follows:

<span id="msg_count"></span>
<span id="notification"></span>

The issue I am facing is that msg_count is showing fine. Now notification is a loop, which means it contains more than one record. When I am calling it through span, it's just showing one record. The rest of the records are not showing. I have given an alert(not) which is alerting all the records. I have only problem in showing in html as <span id="notification"></span>. Can anyone help me?

1
  • Have you tried debugging it? When you are in the $.each loop and when you first iterate over it does $('#notification') have the "not" value? Also after the loop iterates three times the html function seems to override the previous value of the span. Commented Feb 5, 2016 at 10:30

1 Answer 1

1

If notification is showing loop data then you have to add all not in not variable then show it in notification span. The code can as follows:

 function addmsg(type, msg) {
        var obj = jQuery.parseJSON(msg);
        var not = "";
        $.each(obj, function(index, value) {
            var count = value.count;
            not += value.not+"<br/>";
            $('#msg_count').html(count);

        });
    $('#notification').html(not);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

working perfect. :) How will i give a break after each record..Now its showing all together..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.