PHP - Simple Load More Data using jQuery

Submitted by: 
Language: 
Visitors have accessed this post 502 times.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.