tags:

views:

39

answers:

2

Firstly i want to say that im NEW with using JSON.

What i wish to do: As im making an commenting system, its only showing 2 comment for each "news". Then if theres more than 2 comments i made a link that doesnt work, with "click here to view rest of comments." Now i know theres a solution 1) by making a hidden div and then toggle it when someone clicks it. But that would be too much if every news with all the comments should load..

So i wish to use JSON/ajax to send a call to getComments.php, and then response back all comments and then prepend in a div.

So this is what i have right now:

function getComments(id){
    $.ajax({
      url: "misc/getComments.php",
      type: "POST",
      data: { mode: 'ajax', id: id},
        dataType: 'json',
      success: function(data, status){
                    if(typeof(data.error) != 'undefined') {
                        if(data.error != '')
                            alert(data.error);
                    } else if(data.msg == 'OK') {
alert('ok');
      }

      }
    });
}

id in getComments() is the newsid, that will be used in misc/getcomments.php to the SELECT query.

As you can see it should response "OK" if everythings ok, did this with:

echo '{';
   echo ' "msg": "OK" ';
echo '}';

Now, i need to response back with all the info too. The comment, the newsid and other stuff from the database. How should i do that? Should i just add more of these:

echo '{';
   echo ' "comment": "blabla" ';
echo '}';

    echo '{';
       echo ' "id": "1" ';
    echo '}';

? And how can i then fadein the comment that is in a div?

A: 

you can do that, or use the encode function

k.honsali
I assume that is part of PHP, other libraries exist here: http://gggeek.altervista.org/sw/article_20061113.html
k.honsali
A: 

Most probably you can do something like this on the php side.

$comment_items = array();
array_push($comment_items,"This is comment 1");
array_push($comment_items,"This is comment 2");
array_push($comment_items,"This is comment 2");
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
echo json_encode($comment_items);
xar