PHP - Simple Load More Data using jQuery

In this tutorial we will create a Simple Load More Data using jQuery. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. It is designed to make it easier to navigate a document, via classes and id's attribute. So let's now do the coding...

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_data, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_data');
  3.  
  4. if(!$conn){
  5. die("Error: Cannot Connect To Dabase");
  6. }
  7. ?>

Creating The Interface

This is where we will create the appearance of an application. To create this simply copy and write this block of code inside the text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Load More Data using jQuery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Book</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Title</ht>
  23. <th>Description</ht>
  24. <th>Category</ht>
  25. <th>Author</ht>
  26. <th>Date Published</ht>
  27. <th>Quantity</ht>
  28. </tr>
  29. </thead>
  30. <tbody id= "data_table" style="background-color:#fff;">
  31. <?php
  32. require 'conn.php';
  33.  
  34. $query = $conn->query("SELECT * FROM `book` LIMIT 2");
  35.  
  36. while($fetch = $query->fetch_array()){
  37. ?>
  38. <tr>
  39. <?php
  40. $book_id = $fetch['book_id'];
  41. ?>
  42. <td><?php echo $fetch['book_title']?></td>
  43. <td><?php echo $fetch['book_description']?></td>
  44. <td><?php echo $fetch['book_category']?></td>
  45. <td><?php echo $fetch['book_author']?></td>
  46. <td><?php echo $fetch['date_publish']?></td>
  47. <td><?php echo $fetch['qty']?></td>
  48. </tr>
  49.  
  50. <?php
  51. }
  52. ?>
  53. <tr id="remove_row">
  54. <td colspan="6"><center><button id="load" class="btn btn-info btn-block" data-id="<?php echo $book_id?>">Load More</button></center></td>
  55. </tr>
  56. </tbody>
  57. </table>
  58. </div>
  59. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  60. <div class="modal-dialog" role="document">
  61. <form action="save_query.php" method="POST">
  62. <div class="modal-content">
  63. <div class="modal-body">
  64. <div class="col-md-2"></div>
  65. <div class="col-md-8">
  66. <div class="form-group">
  67. <label>Title</label>
  68. <input type="text" class="form-control" name="book_title"/>
  69. </div>
  70. <div class="form-group">
  71. <label>Description</label>
  72. <input type="text" class="form-control" name="book_description"/>
  73. </div>
  74. <div class="form-group">
  75. <label>Category</label>
  76. <input type="text" class="form-control" name="book_category"/>
  77. </div>
  78. <div class="form-group">
  79. <label>Author</label>
  80. <input type="text" class="form-control" name="book_author"/>
  81. </div>
  82. <div class="form-group">
  83. <label>Date Published</label>
  84. <input type="date" class="form-control" name="date_published"/>
  85. </div>
  86. <div class="form-group">
  87. <label>Quantity</label>
  88. <input type="number" class="form-control" name="qty"/>
  89. </div>
  90. </div>
  91. </div>
  92. <div style="clear:both;"></div>
  93. <div class="modal-footer">
  94. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  95. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  96. </div>
  97. </div>
  98. </form>
  99. </div>
  100. </div>
  101. </body>
  102. <script src="js/jquery-3.2.1.min.js"></script>
  103. <script src="js/bootstrap.js"></script>
  104. <script type="text/javascript">
  105. $(document).ready(function(){
  106. $(document).on('click', '#load', function(){
  107. var last_row = $(this).data('id');
  108. $.ajax({
  109. url: 'data.php',
  110. method: 'POST',
  111. data: {last_row: last_row},
  112. success: function(data){
  113. if(data != ""){
  114. $('#remove_row').html('<td colspan="6"><center>Loading...</center></td>');
  115. setTimeout(function(){
  116. $('#remove_row').remove();
  117. $('#data_table').append(data);
  118. }, 3000);
  119. }else{
  120. $('#load').html('No Data');
  121. }
  122. }
  123. });
  124.  
  125. });
  126. });
  127. </script>
  128. </html>

Creating PHP Query

This code contains the query of the application.This code will display the data from the database based on the ajax request. And also can store the data from the inputs to database vice-versa. To do that copy and write these block of codes inside your text editor and save it as shown below. save_query.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. if(!empty($_POST['book_title']) || !empty($_POST['book_description']) || !empty($_POST['book_category']) || !empty($_POST['book_author']) || !empty($_POST['date_published']) || !empty($_POST['qty'])){
  6. $book_title = $_POST['book_title'];
  7. $book_description = addslashes($_POST['book_description']);
  8. $book_category = $_POST['book_category'];
  9. $book_author = $_POST['book_author'];
  10. $date_published = $_POST['date_published'];
  11. $qty = $_POST['qty'];
  12.  
  13. echo $book_description;
  14. $conn->query("INSERT INTO `book` VALUES('', '$book_title', '$book_description', '$book_category', '$book_author', '$date_published', '$qty')");
  15.  
  16. header("location: index.php");
  17. }else{
  18. echo "<script>alert('Please complete the required field!')</script>";
  19. echo "<script>window.location='index.php'</script>";
  20. }
  21. }
  22. ?>
data.php
  1. <?php
  2. require_once 'conn.php';
  3. $output = "";
  4. $id = "";
  5. if(ISSET($_POST['last_row'])){
  6. $query = $conn->query("SELECT * FROM `book` WHERE `book_id` > ".$_POST['last_row']." LIMIT 2");
  7. $rows = $query->num_rows;
  8. if($rows > 0){
  9. while($fetch = $query->fetch_array()){
  10. $id = $fetch['book_id'];
  11. $output .="
  12. <tr>
  13. <td>".$fetch['book_title']."</td>
  14. <td>".$fetch['book_description']."</td>
  15. <td>".$fetch['book_category']."</td>
  16. <td>".$fetch['book_author']."</td>
  17. <td>".$fetch['date_publish']."</td>
  18. <td>".$fetch['qty']."</td>
  19. </tr>
  20. ";
  21. }
  22.  
  23. $output.="
  24. <tr id='remove_row'>
  25. <td colspan='6'><center><button id='load' class='btn btn-info btn-block' data-id='".$id."'>Load More</button></center></td>
  26. </tr>
  27. ";
  28.  
  29. echo $output;
  30. }
  31. }
  32. ?>

Creating The Ajax Funcition

This is where the code that uses ajax script. This code contains several functionalities that need to send request to the php server. To do that just simply copy and write this block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2. $(document).on('click', '#load', function(){
  3. var last_row = $(this).data('id');
  4. $.ajax({
  5. url: 'data.php',
  6. method: 'POST',
  7. data: {last_row: last_row},
  8. success: function(data){
  9. if(data != ""){
  10. $('#remove_row').html('<td colspan="6"><center>Loading...</center></td>');
  11. setTimeout(function(){
  12. $('#remove_row').remove();
  13. $('#data_table').append(data);
  14. }, 3000);
  15. }else{
  16. $('#load').html('No Data');
  17. }
  18. }
  19. });
  20.  
  21. });
  22. });
There you have it we successfully created a Simple Load More Data using jQuery using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment