Type something and hit enter

author photo
By On
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. INSERT INTO `tutorial` (`tutorialid`, `title`, `link`) VALUES
  8. (1, 'Submit Form using AJAX in PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11649/submit-form-using-ajax-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11649/submit-form-using-aja...</a>),
  9. (2, 'Image Upload using AJAX in PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11648/image-upload-using-ajax-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11648/image-upload-using-aj...</a>),
  10. (3, 'Creating an Iterator FOR While Loop Forms IN PHP/MySQLi', '<a href="https://www.sourcecodester.com/php/11643/creating-iterator-while-loop-forms-phpmysqli.html'">https://www.sourcecodester.com/php/11643/creating-iterator-while-loop-fo...</a>),
  11. (4, 'Drag, Drop and Insert into Database using AJAX/jQuery in PHP', '<a href="https://www.sourcecodester.com/tutorials/php/11641/drag-drop-and-insert-database-using-ajaxjquery-php.html'">https://www.sourcecodester.com/tutorials/php/11641/drag-drop-and-insert-...</a>),
  12. (5, 'Simple POS and Inventory System', '<a href="https://www.sourcecodester.com/php/11625/simple-pos-and-inventory-system.html'">https://www.sourcecodester.com/php/11625/simple-pos-and-inventory-system...</a>),
  13. (6, 'Performance Indicator System', '<a href="https://www.sourcecodester.com/php/11638/performance-indicator-system.html'">https://www.sourcecodester.com/php/11638/performance-indicator-system.html'</a>),
  14. (7, 'Simple Chat System', '<a href="https://www.sourcecodester.com/php/11610/simple-chat-system.html'">https://www.sourcecodester.com/php/11610/simple-chat-system.html'</a>),
  15. (8, 'Uploading Multiple Files INTO MySQL DATABASE USING PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11634/uploading-multiple-files-mysql-database-using-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11634/uploading-multiple-fi...</a>),
  16. (9, 'Deleting Multiple Rows using Checkbox in PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11631/deleting-multiple-rows-using-checkbox-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11631/deleting-multiple-row...</a>),
  17. (10, 'PHP/MySQLi CRUD Operation with Bootstrap/Modal', '<a href="https://www.sourcecodester.com/php/11629/phpmysqli-crud-operation-bootstrapmodal.html'">https://www.sourcecodester.com/php/11629/phpmysqli-crud-operation-bootst...</a>),
  18. (11, 'PHP Passing VALUE TO Modal USING jQuery', '<a href="https://www.sourcecodester.com/tutorials/php/11627/php-passing-value-modal-using-jquery.html'">https://www.sourcecodester.com/tutorials/php/11627/php-passing-value-MOD...</a>),
  19. (12, 'How to Add Class to a Div using JQuery', '<a href="https://www.sourcecodester.com/tutorials/javascript/11619/how-add-class-div-using-jquery.html'">https://www.sourcecodester.com/tutorials/javascript/11619/how-add-class-...</a>),
  20. (13, 'How to Limit Number of Items per Row in While Loop using PHP/MySQLi', '<a href="https://www.sourcecodester.com/tutorials/php/11618/how-limit-number-items-row-while-loop-using-phpmysqli.html'">https://www.sourcecodester.com/tutorials/php/11618/how-limit-number-item...</a>),
  21. (14, 'PHP Prevent the RETURN TO Login Page/Disable Back after Login', '<a href="https://www.sourcecodester.com/tutorials/php/11614/php-prevent-return-login-pagedisable-back-after-login.html'">https://www.sourcecodester.com/tutorials/php/11614/php-prevent-return-lo...</a>);
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. $conn = mysqli_connect("localhost","root","","testing");
  3. if (!$conn) {
  4. die("Connection failed: " . mysqli_connect_error());
  5. }
  6. ?>

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="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"" rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"</a> />
  6. <script src="<a href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>" rel="nofollow">https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></s...</a>
  7. <script src="<a href="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>" rel="nofollow">https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script></a>
  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. <div id="remove">
  32. <div style="height:10px;"></div>
  33. <div class="row">
  34. <div class="col-lg-12">
  35. <button type="button" name="loadmore" id="loadmore" data-id="<?php echo $lastid; ?>" class="btn btn-primary">See More</button>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <script src="custom.js"></script>
  43. </body>
  44. </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. if(mysqli_num_rows($query) > 0){
  8. while($row = mysqli_fetch_array($query)){
  9. ?>
  10. <div class="row">
  11. <div class="col-lg-12">
  12. <a href="<?php echo $row['link']; ?>"><?php echo $row['title']; ?></a>
  13. </div>
  14. </div>
  15. <?php
  16. $lastid=$row['tutorialid'];
  17. }
  18. ?>
  19. <div id="remove">
  20. <div style="height:10px;"></div>
  21. <div id="remove_row" class="row">
  22. <div class="col-lg-12">
  23. <button type="button" name="loadmore" id="loadmore" data-id="<?php echo $lastid; ?>" class="btn btn-primary">See More</button>
  24. </div>
  25. </div>
  26. </div>
  27. <?php
  28. }
  29. }
  30. ?>
That ends this tutorial. If you have any questions or comments, feel free to write it below or message me. Happy Coding :)

Click to comment