PHP - Remove Table Row Using Ajax

In this tutorial we will create a Remove Table Row using Ajax. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. Using Ajax, data could then be passed between the browser and the server, using the XMLHttpRequest API, without having to reload the web page. So Let's do the coding...

Getting 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_ajax, 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 = mysqli_connect('localhost', 'root', '', 'db_ajax') or die(mysqli_error());
  3. if(!$conn){
  4. die("Error: Failed to connect to database");
  5. }
  6. ?>

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 - Remove Table Row Using Ajax</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-success" data-target="#form_modal" data-toggle="modal"><span class="glyphicon glyphicon-plus"></span> Add Student</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Gender</th>
  25. <th>Age</th>
  26. <th>Action</th>
  27. </tr>
  28. </thead>
  29. <tbody style="background-color:#fff;">
  30. <?php
  31. require 'conn.php';
  32. $query = mysqli_query($conn, "SELECT * FROM `student` ORDER BY `lastname` ASC") or die(mysqli_error());
  33. while($fetch = mysqli_fetch_array($query)){
  34. ?>
  35. <tr class="del_stud<?php echo $fetch['stud_id']?>">
  36. <td><?php echo $fetch['firstname']?></td>
  37. <td><?php echo $fetch['lastname']?></td>
  38. <td><?php echo $fetch['gender']?></td>
  39. <td><?php echo $fetch['age']?></td>
  40. <td><button type="button" class="btn btn-danger btn_del" id="<?php echo $fetch['stud_id']?>"><span class="glyphicon glyphicon-remove"></span> Delete</button></td>
  41. </tr>
  42. <?php
  43. }
  44. ?>
  45. </tbody>
  46. </table>
  47. </div>
  48. <div class="modal fade" id="modal_confirm" aria-hidden="true">
  49. <div class="modal-dialog modal-dialog-centered">
  50. <div class="modal-content">
  51. <div class="modal-header">
  52. <h3 class="modal-title">System</h3>
  53. </div>
  54. <div class="modal-body">
  55. <center><h4>Are you sure you want to delete this data?</h4></center>
  56. </div>
  57. <div class="modal-footer">
  58. <button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
  59. <button type="button" class="btn btn-success" id="btn_yes">Yes</button>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. <div class="modal fade" id="form_modal" aria-hidden="true">
  65. <div class="modal-dialog">
  66. <div class="modal-content">
  67. <form method="POST" action="save_student.php">
  68. <div class="modal-header">
  69. <h3 class="modal-title">Add Student</h3>
  70. </div>
  71. <div class="modal-body">
  72. <div class="col-md-2"></div>
  73. <div class="col-md-8">
  74. <div class="form-group">
  75. <label>Firstname</label>
  76. <input type="text" name="firstname" class="form-control" required="required"/>
  77. </div>
  78. <div class="form-group">
  79. <label>Lastname</label>
  80. <input type="text" name="lastname" class="form-control" required="required" />
  81. </div>
  82. <div class="form-group">
  83. <label>Gender</label>
  84. <select name="gender" class="form-control" required="required">
  85. <option value="">Select an option</option>
  86. <option value="Male">Male</option>
  87. <option value="Female">Female</option>
  88. </select>
  89. </div>
  90. <div class="form-group">
  91. <label>Age</label>
  92. <input type="number" name="age" class="form-control" min="0" max="200" required="required" />
  93. </div>
  94. </div>
  95. </div>
  96. <div style="clear:both;"></div>
  97. <div class="modal-footer">
  98. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  99. <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  100. </div>
  101. </div>
  102. </form>
  103. </div>
  104. </div>
  105. </div>
  106. <script src="js/jquery-3.2.1.min.js"></script>
  107. <script src="js/bootstrap.js"></script>
  108. <script src="js/script.js"></script>
  109. </body>
  110. </html>

Creating PHP Query

This code contains the php query of the application. This code will save the data inputs to the MySQLi database server, and also has a query that let the user to delete the data on the table. To do that just copy and write this block of codes inside the text editor, then save it as shown below. save_student.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $firstname = $_POST['firstname'];
  5. $lastname = $_POST['lastname'];
  6. $gender = $_POST['gender'];
  7. $age = $_POST['age'];
  8.  
  9. mysqli_query($conn, "INSERT INTO `student` VALUES('', '$firstname', '$lastname', '$gender', '$age')") or die(mysqli_error());
  10.  
  11. header("location: index.php");
  12. ?>
remove_student.php
  1. <?php
  2. require_once 'conn.php';
  3. $stud_id = $_POST['stud_id'];
  4. mysqli_query($conn, "DELETE FROM `student` WHERE `stud_id` = $stud_id") or die(mysqli_error());
  5. ?>

Creating the Script

This is where the main function of the application.This code will clear the deleted table row by sending an ajax request to the php server. 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. $('.btn_del').on('click', function(){
  3. var stud_id = $(this).attr('id');
  4. $("#modal_confirm").modal('show');
  5. $('#btn_yes').attr('name', stud_id);
  6. });
  7. $('#btn_yes').on('click', function(){
  8. var id = $(this).attr('name');
  9. $.ajax({
  10. type: "POST",
  11. url: "remove_student.php",
  12. data:{
  13. stud_id: id
  14. },
  15. success: function(){
  16. $("#modal_confirm").modal('hide');
  17. $(".del_stud" + id).empty();
  18. $(".del_stud" + id).html("<td colspan='5'><center>Deleting...</center></td>");
  19. setTimeout(function(){
  20. $(".del_stud" + id).fadeOut('slow');
  21. }, 1000);
  22. }
  23. });
  24. });
  25. });
There you have it we successfully created Remove Table Row using Ajax. 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