Load More Data using AJAX/jQuery in PHP/MySQLi

This tutorial will teach you on how to create a load more button that loads additional data using AJAX/jQuery. Note: Scripts and css used in this tutorial are hosted, therefore, you need internet connection for them to work.

Creating our Database

First step is to create our database. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as testing. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `tutorial` (
  2. `tutorialid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `title` VARCHAR(150) NOT NULL,
  4. `link` VARCHAR(150) NOT NULL,
  5. PRIMARY KEY(`tutorialid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  7.  
  8. INSERT INTO `tutorial` (`tutorialid`, `title`, `link`) VALUES
  9. (1, 'Submit Form using AJAX in PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11649/submit-form-using-ajax-phpmysqli.html'),
  10. (2, 'Image Upload using AJAX in PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11648/image-upload-using-ajax-phpmysqli.html'),
  11. (3, 'Creating an Iterator for While Loop Forms in PHP/MySQLi', 'https://www.sourcecodester.com/php/11643/creating-iterator-while-loop-forms-phpmysqli.html'),
  12. (4, 'Drag, Drop and Insert into Database using AJAX/jQuery in PHP', 'https://www.sourcecodester.com/tutorials/php/11641/drag-drop-and-insert-database-using-ajaxjquery-php.html'),
  13. (5, 'Simple POS and Inventory System', 'https://www.sourcecodester.com/php/11625/simple-pos-and-inventory-system.html'),
  14. (6, 'Performance Indicator System', 'https://www.sourcecodester.com/php/11638/performance-indicator-system.html'),
  15. (7, 'Simple Chat System', 'https://www.sourcecodester.com/php/11610/simple-chat-system.html'),
  16. (8, 'Uploading Multiple Files into MySQL Database using PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11634/uploading-multiple-files-mysql-database-using-phpmysqli.html'),
  17. (9, 'Deleting Multiple Rows using Checkbox in PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11631/deleting-multiple-rows-using-checkbox-phpmysqli.html'),
  18. (10, 'PHP/MySQLi CRUD Operation with Bootstrap/Modal', 'https://www.sourcecodester.com/php/11629/phpmysqli-crud-operation-bootstrapmodal.html'),
  19. (11, 'PHP Passing Value to Modal using jQuery', 'https://www.sourcecodester.com/tutorials/php/11627/php-passing-value-modal-using-jquery.html'),
  20. (12, 'How to Add Class to a Div using JQuery', 'https://www.sourcecodester.com/tutorials/javascript/11619/how-add-class-div-using-jquery.html'),
  21. (13, 'How to Limit Number of Items per Row in While Loop using PHP/MySQLi', 'https://www.sourcecodester.com/tutorials/php/11618/how-limit-number-items-row-while-loop-using-phpmysqli.html'),
  22. (14, 'PHP Prevent the Return to Login Page/Disable Back after Login', 'https://www.sourcecodester.com/tutorials/php/11614/php-prevent-return-login-pagedisable-back-after-login.html');
loadmore

Creating our Connection

Next, we create our connection to our database. This will serve as the bridge between our forms and database. We name this as conn.php.
  1. <?php
  2.  
  3. $conn = mysqli_connect("localhost","root","","testing");
  4. if (!$conn) {
  5. die("Connection failed: " . mysqli_connect_error());
  6. }
  7.  
  8. ?>

index.php

Next step is to create our initial data from database then our load more button. Notice that we set our initial data to fetch only 2 rows from our database.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Load More Data using Ajax/Jquery in PHP/MySQLi</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  6. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  7. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div>
  12. <h2 align="center" >Load More Data using Ajax/Jquery in PHP/MySQLi</h2>
  13. <div style="height:10px;"></div>
  14. <h4>My Tutorials and Source Codes</h4>
  15. <div id="loadtable">
  16. <?php
  17. $lastid='';
  18. include('conn.php');
  19. $query=mysqli_query($conn,"select * from tutorial order by tutorialid asc limit 2");
  20. while($row=mysqli_fetch_array($query)){
  21. ?>
  22. <div class="row">
  23. <div class="col-lg-12">
  24. <a href="<?php echo $row['link']; ?>"><?php echo $row['title']; ?></a>
  25. </div>
  26. </div>
  27. <?php
  28. $lastid=$row['tutorialid'];
  29. }
  30.  
  31. ?>
  32. <div id="remove">
  33. <div style="height:10px;"></div>
  34. <div class="row">
  35. <div class="col-lg-12">
  36. <button type="button" name="loadmore" id="loadmore" data-id="<?php echo $lastid; ?>" class="btn btn-primary">See More</button>
  37. </div>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43.  
  44. <script src="custom.js"></script>
  45. </body>
  46. </html>

custom.js

This contains our AJAX and jQuery codes that fetch additional data from our database.
  1. $(document).ready(function(){
  2. $(document).on('click', '#loadmore', function(){
  3. var lastid = $(this).data('id');
  4. $('#loadmore').html('Loading...');
  5. $.ajax({
  6. url:"load_data.php",
  7. method:"POST",
  8. data:{
  9. lastid:lastid,
  10. },
  11. dataType:"text",
  12. success:function(data)
  13. {
  14. if(data != '')
  15. {
  16. $('#remove').remove();
  17. $('#loadtable').append(data);
  18. }
  19. else
  20. {
  21. $('#loadmore').html('No more data to show');
  22. }
  23. }
  24. });
  25. });
  26. });

load_data.php

Lastly, we create our code in fetching additional data from our database.
  1. <?php
  2. sleep(1);
  3. include('conn.php');
  4. if(isset($_POST['lastid'])){
  5. $lastid=$_POST['lastid'];
  6. $query=mysqli_query($conn,"select * from tutorial where tutorialid > '$lastid' order by tutorialid asc limit 2");
  7.  
  8. if(mysqli_num_rows($query) > 0){
  9. while($row = mysqli_fetch_array($query)){
  10. ?>
  11. <div class="row">
  12. <div class="col-lg-12">
  13. <a href="<?php echo $row['link']; ?>"><?php echo $row['title']; ?></a>
  14. </div>
  15. </div>
  16. <?php
  17. $lastid=$row['tutorialid'];
  18. }
  19. ?>
  20. <div id="remove">
  21. <div style="height:10px;"></div>
  22. <div id="remove_row" class="row">
  23. <div class="col-lg-12">
  24. <button type="button" name="loadmore" id="loadmore" data-id="<?php echo $lastid; ?>" class="btn btn-primary">See More</button>
  25. </div>
  26. </div>
  27. </div>
  28. <?php
  29. }
  30. }
  31. ?>
That ends this tutorial. If you have any questions or comments, feel free to write it below or message me. Happy Coding :)

Add new comment