PHP - Delete Data Without Page Refresh Using MySQLi

Language
In this tutorial we will create a Delete Data Without Page Refresh using MySQLi Using MySQLi Using PHP. PHP is a server-side scripting language designed primarily for web development. It is mostly used by a newly coders for its user friendly environment. So let's now 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_member, 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_member') 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 - Delete Data Without Page Refresh Using MySQLi</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 Member</button> 
  18.                 <br /><br />
  19.                 <table class="table table-bordered">
  20.                         <thead>
  21.                                 <tr>
  22.                                         <th>Firstname</th>
  23.                                         <th>Lastname</th>
  24.                                         <th>Age</th>
  25.                                         <th>Address</th>
  26.                                         <th>Civil Status</th>
  27.                                         <th>Action</th>
  28.                                 </tr>
  29.                         </thead>
  30.                         <tbody>
  31.                                 <?php
  32.                                         require 'conn.php';
  33.                                         $query = mysqli_query($conn, "SELECT * FROM `member` ORDER BY `lastname` ASC") or die(mysqli_error());
  34.                                         while($fetch = mysqli_fetch_array($query)){
  35.                                 ?>
  36.                                 <tr class="del_mem<?php echo $fetch['mem_id']?>">
  37.                                         <td><?php echo $fetch['firstname']?></td>
  38.                                         <td><?php echo $fetch['lastname']?></td>
  39.                                         <td><?php echo $fetch['age']?></td>
  40.                                         <td><?php echo $fetch['address']?></td>
  41.                                         <td><?php echo $fetch['civil_status']?></td>
  42.                                         <td><button type="button" class="btn btn-danger btn_del" id="<?php echo $fetch['mem_id']?>"><span class="glyphicon glyphicon-remove"></span> Delete</button></td>
  43.                                 </tr>
  44.                                 <?php
  45.                                         }
  46.                                 ?>
  47.                         </tbody>
  48.                 </table>
  49.         </div>
  50.         <div class="modal fade" id="modal_confirm" aria-hidden="true">
  51.                 <div class="modal-dialog modal-dialog-centered">
  52.                         <div class="modal-content">
  53.                                 <div class="modal-header">
  54.                                         <h3 class="modal-title">System</h3>
  55.                                 </div>
  56.                                 <div class="modal-body">
  57.                                         <center><h4>Are you sure you want to delete this data?</h4></center>
  58.                                 </div>
  59.                                 <div class="modal-footer">
  60.                                         <button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
  61.                                         <button type="button" class="btn btn-success" id="btn_yes">Yes</button>
  62.                                 </div>
  63.                         </div>
  64.                 </div>
  65.         </div>
  66.         <div class="modal fade" id="form_modal" aria-hidden="true">
  67.                 <div class="modal-dialog">
  68.                         <div class="modal-content">
  69.                                 <form method="POST" action="save_member.php">
  70.                                         <div class="modal-header">
  71.                                                 <h3 class="modal-title">Add Member</h3>
  72.                                         </div>
  73.                                         <div class="modal-body">
  74.                                                 <div class="col-md-2"></div>
  75.                                                 <div class="col-md-8">
  76.                                                         <div class="form-group">
  77.                                                                 <label>Firstname</label>
  78.                                                                 <input type="text" name="firstname" class="form-control" required="required"/>
  79.                                                         </div>
  80.                                                         <div class="form-group">
  81.                                                                 <label>Lastname</label>
  82.                                                                 <input type="text" name="lastname" class="form-control" required="required" />
  83.                                                         </div>
  84.                                                         <div class="form-group">
  85.                                                                 <label>Age</label>
  86.                                                                 <input type="number" name="age" class="form-control" min="0" max="200" required="rquired" />
  87.                                                         </div>
  88.                                                         <div class="form-group">
  89.                                                                 <label>Address</label>
  90.                                                                 <input type="text" name="address" class="form-control" required="required"/>
  91.                                                         </div>
  92.                                                         <div class="form-group">
  93.                                                                 <label>Civil Status</label>
  94.                                                                 <input type="text" name="civil_status" class="form-control" required="required"/>
  95.                                                         </div>
  96.                                                 </div>
  97.                                         </div>
  98.                                         <div style="clear:both;"></div>
  99.                                         <div class="modal-footer">
  100.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  101.                                                 <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  102.                                         </div>
  103.                                         </div>
  104.                                 </form>
  105.                         </div>
  106.                 </div>
  107.         </div>
  108. <script src="js/jquery-3.2.1.min.js"></script>
  109. <script src="js/bootstrap.js"></script>
  110. <script src="js/script.js"></script>
  111. </body>
  112. </html>

Creating PHP Query

This code contains the php query of the application. This code will save the user data inputs into the database server.. To do that just copy and write this block of codes inside the text editor, then save it as save_member.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         $firstname = $_POST['firstname'];
  5.         $lastname = $_POST['lastname'];
  6.         $age = $_POST['age'];
  7.         $address = $_POST['address'];
  8.         $civil_status = $_POST['civil_status'];
  9.        
  10.         mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$age', '$address', '$civil_status')") or die(mysqli_error());
  11.        
  12.         header("location: index.php");
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will delete the data in the table without refreshing the web page using Ajax. The jQuery Ajax will take care of all the incoming request from php to prevent default refresh of the web browser. To do that just kindly copy and write these block of codes inside the text editor then save it as shown below. delete_member.php
  1. <?php
  2.         require_once 'conn.php';
  3.         $mem_id = $_POST['mem_id'];
  4.         mysqli_query($conn, "DELETE FROM `member` WHERE `mem_id` = $mem_id") or die(mysqli_error());
  5. ?>
Save this javascript file inside the js directory to have an absolute file path. script.js
  1. $(document).ready(function(){
  2.         $('.btn_del').on('click', function(){
  3.                 var mem_id = $(this).attr('id');
  4.                 $("#modal_confirm").modal('show');
  5.                 $('#btn_yes').attr('name', mem_id);
  6.         });
  7.         $('#btn_yes').on('click', function(){
  8.                 var id = $(this).attr('name');
  9.                 $.ajax({
  10.                         type: "POST",
  11.                         url: "delete_member.php",
  12.                         data:{
  13.                                 mem_id: id
  14.                         },
  15.                         success: function(){
  16.                                 $("#modal_confirm").modal('hide');
  17.                                 $(".del_mem" + id).empty();
  18.                                 $(".del_mem" + id).html("<td colspan='6'><center>Deleting...</center></td>");
  19.                                 setTimeout(function(){
  20.                                         $(".del_mem" + id).fadeOut('slow');
  21.                                 }, 1000);
  22.                         }
  23.                 });
  24.         });
  25. });
There you have it we successfully created Delete Data Without Page Refresh Using MySQLi. 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment