You might have seen the implementation of Ajax based data loading system at Facebook, Twitter or some other sites. Instead of showing pagination links, we can load data dynamically whenever a load button click. In this article I’ll show you how we can easily adopt this technique to make similar loading page for our web projects using jQuery.
You may also want to checkout similar tutorial Auto Load More Data On Page Scroll. In my earlier post Simple Ajax Pagination, I have explained how to create Ajax Pagination using jQuery. Here we will be doing precisely the same thing.
We use this configuration file to store certain variables such as MySql username and password, which is required by our program..
Since we will be sending data as <LI> list item, we can create a <UL> element with id “results”, here we will load our data fetched from server.
Here’s our jQuery code, when page loads we need to load the first set of data, once it’s loaded we can add a button which can initiate the remaining data loading until all the records are loaded. You can also change the settings options such as loading image URL, button label etc.
As you can see we are utilizing jQuery.post() to load data from server, which is a shorthand Ajax function of $.ajax().
As I discussed earlier, we need to pass the page number to the PHP script below, it then will connect to database and fetch the records using page number and “item_per_page” offset. The PHP code uses MySqli Prepared Statements to fetch the records, which is very resilient against harmful injection practices, and the performance is also highly efficient. You can learn more about MySqli Prepared Statements here.
That’s it! we should have a neat jQuery based data loading page by now. Don’t forget to check out the demo and download the whole thing on your computer. Good luck! If you want to make Ajax Pagination with Next-Previous buttons, you can check out this updated article — Ajax Pagination with jQuery & PHP.