PHP - Auto Update Content Using jQuery

In this tutorial we will create a Auto Update Content 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 get 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_update, 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(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_update');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your 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 - Auto Update Content Using jQuery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal">Add Customer</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-warning">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Address</th>
  25. </tr>
  26. </thead>
  27. <tbody id="data">
  28.  
  29. </tbody>
  30. </table>
  31. </div>
  32.  
  33. <div class="modal fade" id="form_modal" aria-hidden="true">
  34. <div class="modal-dialog" role="document">
  35. <form>
  36. <div class="modal-content">
  37. <div class="modal-body">
  38. <div class="col-md-2"></div>
  39. <div class="col-md-8">
  40. <div class="form-group">
  41. <label>Firstname</label>
  42. <input class="form-control" type="text" id="firstname"/>
  43. </div>
  44. <div class="form-group">
  45. <label>Lastname</label>
  46. <input class="form-control" type="text" id="lastname"/>
  47. </div>
  48. <div class="form-group">
  49. <label>Address</label>
  50. <input class="form-control" type="text" id="address" />
  51. </div>
  52. </div>
  53. </div>
  54. <div style="clear:both;"></div>
  55. <div class="modal-footer">
  56. <button type="button" id="close" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  57. <button type="button" id="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  58. </div>
  59. </div>
  60. </form>
  61. </div>
  62. </div>
  63. </body>
  64. <script src="js/jquery-3.2.1.min.js"></script>
  65. <script src="js/bootstrap.js"></script>
  66. <script src="js/script.js"></script>
  67. </html>

Creating PHP Query

This code contains the php query of the application. This code will send the data input to the database server, and then display the data to the Wepage using ajax request. To do that just copy and write this block of codes inside the text editor, then save it as shown below. save_query.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $firstname = $_POST['firstname'];
  5. $lastname = $_POST['lastname'];
  6. $address = $_POST['address'];
  7.  
  8. $conn->query("INSERT INTO `customer` VALUES('', '$firstname', '$lastname', '$address')");
  9.  
  10. echo "Data Inserted!";
  11. ?>
data.php
  1. <?php
  2. require 'conn.php';
  3.  
  4. $query = $conn->query("SELECT * FROM `customer`");
  5. while($fetch = $query->fetch_array()){
  6. ?>
  7. <tr>
  8. <td><?php echo $fetch['firstname']?></td>
  9. <td><?php echo $fetch['lastname']?></td>
  10. <td><?php echo $fetch['address']?></td>
  11. </tr>
  12. <?php
  13. }
  14. ?>

Creating jQuery Script

This is where the main function of the application.This code update the table data every 30 seconds interval. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2.  
  3. DisplayData();
  4. Update(); // Update every 30 Seconds
  5.  
  6. $('#save').on('click', function(){
  7. var firstname = $('#firstname').val();
  8. var lastname = $('#lastname').val();
  9. var address = $('#address').val();
  10.  
  11. if(firstname == "" || lastname == "" || address ==""){
  12. alert('Please complete the required field!');
  13. }else{
  14.  
  15. $.ajax({
  16. url: 'save_query.php',
  17. type: 'POST',
  18. data: {firstname: firstname, lastname: lastname, address: address},
  19. success: function(data){
  20. alert(data);
  21. $('#close').click();
  22. DisplayData();
  23. $('#firstname').val('');
  24. $('#lastname').val('');
  25. $('#address').val('');
  26. }
  27. });
  28. }
  29. });
  30.  
  31. function DisplayData(){
  32. $.ajax({
  33. url: 'data.php',
  34. type: 'POST',
  35. data: {res:1},
  36. success: function(data){
  37. $('#data').html(data);
  38. }
  39. });
  40. }
  41.  
  42. function Update(){
  43. setTimeout(function(){
  44. DisplayData();
  45. Update();
  46. }, 30000);
  47. }
  48. });
There you have it we successfully created Auto Update Content Using jQuery. 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