views:

175

answers:

2

I am trying to create a commenting system whereby admins are able to moderate comments and users are able to post comments all in the one page load. I have all the backend knowledge on how to delete the comments, but I really have to know how the new comments could be loaded from the database and shown on the current page.

Edit: More specifically what would the actual javascript look like that would render the new replies?

+2  A: 

You'll want a periodic AJAX call to poll the server for new comments.

You should have a datetime of the latest answer the browser knows and the question ID to query the server, to optimize your traffic.

Using JQuery:

var refreshId = setInterval(function() {
     $.getJSON("http://server.com/form?question=" + questionId + "?time=" + datetimeStamp",
        function(data){
          $.each(data.items, function(i,item){
            $("#comments").append( // new div with content)
          });
        });

}, 3000);
Nerdling
I prefer the JSON functions, but you can use the regular AJAX get as well.
Nerdling
Any way to do it without JQuery?
Sam152
Yes, you can manually write all your AJAX calls, do getElementByIds and loop through everything … it's just way easier with JQuery.
Nerdling
A: 

Or you could avoid polling and make the server push the data down to the client. This is how GTalk or Meebo works by pushing new messages. Comet (pushing or streaming data) support is built-in with some JavaScript frameworks like Dojo or a jQuery plugin.

See http://ajaxian.com/archives/comet-a-new-approach-to-ajax-applications http://plugins.jquery.com/project/Comet

Cesar
Anything native to javascript?
Sam152
I fail to see the non-native part. There's no additional requirements beside javascript on the client, and the server is basically left untouched.
Cesar
I meant framework independent, but you're right.
Sam152