views:

322

answers:

3

I would like to create a pagination system for comments in my website.So far, I have been able to create pagination using php/mysql and html but the page has to refresh every time we click on the next button(for the next set of comments) or previous or a particular page....

As far as my knowledge of jquery is concerned I am thinking that, when the user clicks on the next button we post data for the page number to comments.php then echo all the comments in comments.php, then the jquery data variable recieves all the data echo'd in the file and appends it to the #comments box...

Is my solution a valid one??? or anyone has a better solution.....thanks

A: 

Yes, when you click 'next', you send ajax request to comments.php and replace current comments with new ones.

usoban
are you certain this is the best way... or have you come across any other methods
halocursed
Well I have used this for a while, but it's also possible to load all comments at once and then hide some of them, as mentioned below. At this case, you don't need additional ajax.
usoban
A: 

You can do it with a get()/getJSON() call in jQuery.

Something like

$('#next').click(function(){
      $.getJSON('url?withnextpage=number',
               function(data){
                   //update variables or the DOM
               });

});

Returning it in JSON may be quicker. I hope that helps

AutomatedTester
+1  A: 

Your question doesn't make much sense and is very jumbled.

You can either load the entire list when the page first loads, and use jquery to paginate it by hiding the extra entries, which will work fine for lists with a few pages worth of content.

The other option is to use AJAX to fetch the next or previous page when the appropriate link is clicked.

There are plenty of pagination add ons for jquery. Maybe check them out.

Don't use a POST request to get the next page as it looks like you might be, unless you are just using the wrong terminology.

Antony Carthy
Why not using POST request?
usoban
I don't want to load the entire list when the page loads, I basically want to use Ajax to fetch the desired set of data and then display it .... sorry if I was not clear enough in the question ..The pagination is working fine at the moment but since I am not using Javascript to paginate, the page has to refresh to display the next set of comments...
halocursed
usoban: You should use a GET request when you are getting a page... and POST when you are sending data to be stored, basically.Atul, You then need to make sure the ajax is working properly - read more on the jQuery ajax functions to make sure you are using them right.
Antony Carthy