0

My jQuery part isn't working, can sombody please explain why I do not get the desired output ? index.php

while ($row = mysql_fetch_array($res))
    array_push($result, array('text' =>$row[1]));
echo json_encode(array("result"=> $result));

getting.js

$.getJson("view_note.php", function(data){
            $("ul").empty();

            $.each(data.result,function(){

            $("ul").append("<li>Text: "+this['text']+"</li><br />";
            });
        });
    });

Output OutPut

5
  • igonre the vertical line Commented Dec 7, 2015 at 8:59
  • You will have to add some debug output. On php side: what does $result finally contain? On JS side: what does data contain? On network level: what is transferred? Those are all things only you can find out, we do not have access to your system... Commented Dec 7, 2015 at 9:02
  • 1
    Lesson 1: Learn to debug my friend. You can use console.log(data); to view what kind of data are you receiving. Another thing I see in your code is you have to remove the <br /> Commented Dec 7, 2015 at 9:09
  • The error is uncaught syntax error Commented Dec 7, 2015 at 9:13
  • there are a syntex error in ur each loop. u can check it in below my answer. Commented Dec 7, 2015 at 9:23

5 Answers 5

1
Try this:

$.getJson("view_note.php", function(data){
    $("ul").empty();

    $.each(data.result,function(){

        $("ul").append("<li>Text: "+this['text']+"</li><br />");
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Use each loop as

$.each(data.result,function(i,item){
            $("ul").append("<li>Text: "+$(item).text+"</li>";
            });

also remove <br/>

Comments

0

I think it's because you wrap the array 2 times.

echo json_encode(array("result"=> $result));

to

echo json_encode($result);

1 Comment

The variable $result is already an array from what I see coz you use array_push to insert the data and then you use echo json_encode(array("result"=> $result)); to put it inside another array. That's why you got the answer still in {}
0

The answer is the syntax (AFAIK), this is your code formatted and with syntax fixed:

$.getJson("view_note.php", function(data){
    $("ul").empty();

    $.each(data.result,function(){
        $("ul").append("<li>Text: "+this['text']+"</li><br />");
    });
});

I'd prefer this to be a comment but I cannot because of the size and formatting constraints

Comments

0

You can change to this:

var data = [{
  "result": [{
    "text": "My first note."
  }, {
    "text": "My Second note."
  }]
}]
$.each(data[0]['result'], function(i, item) {
  $("ul").append("<li>Text: " + item.text + "</li>");
  //-----------add index ----------^-----put closing-^-----here.
  // also no need to add <br> tag at the end.
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul></ul>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.