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?