Delete Data without Refreshing Page

In this tutorial, we are going to create how to Delete Data without Refreshing Page. This is a simple project delete function using PHP/MySQL with AJAX script. This tutorial feature is to delete the data from the table and it also deleting data without any refresh of the page or reload. Hoping that this simple source code will help you a lot. Enjoy coding. Thank you. Create Data Table In the source code below, contains all the data from the database, we have to display all the data. To select individual which data we are going to delete it. Kindly review the source code below.
  1. <tr>
  2. <th>Member Name</th>
  3. <th>Contact</th>
  4. <th>Date Added</th>
  5. <th>Action</th>
  6. </tr>
  7. </thead>
  8. <?php
  9. $result = $database->prepare ("SELECT * FROM tbl_member");
  10. $result ->execute();
  11. for ($count=0; $row_member = $result ->fetch(); $count++){
  12. $id = $row_member['tbl_member_id'];
  13. ?>
  14. <tr class="delete_mem<?php echo $id ?>">
  15. <td><?php echo $row_member['tbl_member_name']; ?></td>
  16. <td><?php echo $row_member['tbl_member_contact']; ?></td>
  17. <td><?php echo date("M d, Y h:i:s A", strtotime ($row_member['tbl_member_added'])); ?></td>
  18. <td width="80">
  19. <a id="<?php echo $id; ?>">Delete</a>
  20. </td>
  21. </tr>
  22. <?php } ?>
  23. </tbody>
Constructing the AJAX script to have an effect when the user deleting data in the table, it will slowly fade when to hit the button delete in the table.
  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $('.btn-danger').click(function() {
  4. var id = $(this).attr("id");
  5. if (confirm("Are you sure you want to delete this Member?")) {
  6. $.ajax({
  7. type: "POST",
  8. url: "delete_member.php",
  9. data: ({
  10. id: id
  11. }),
  12. cache: false,
  13. success: function(html) {
  14. $(".delete_mem" + id).fadeOut('slow');
  15. }
  16. });
  17. } else {
  18. return false;
  19. }
  20. });
  21. });
  22. </script>
And, here's the delete query.
  1. <?php
  2. include ('database.php');
  3.  
  4. $id = $_GET['id'];
  5. $delete_data"delete from tbl_member where tbl_member_id = '$id' ";
  6. $database->exec($delete_data);
  7. ?>

Output

Result

Comments

Add new comment