Easy and Simple Deleting of MySQL Row using AJAX/jQuery

In this tutorial, I'm going to show you how to delete mysql row using ajax and jquery. We use ajax and jquery, so that, we don't need to reload the page after our action. You might want to learn How to Insert Data using Ajax/JQuery. This tutorial will not give you a good design but will give you idea on the topic.

Creating our Database

First, we're going to create our database. This will store our sample data. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as "ajax_insert". 3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
  1. CREATE TABLE `post` (
  2.   `postid` INT(11) NOT NULL AUTO_INCREMENT,
  3.   `post_text` VARCHAR(200) NOT NULL,
  4.   `date_time` datetime NOT NULL,
  5. PRIMARY KEY(`postid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
del_ajax

Inserting our Sample Data

Next Step is to insert our sample data into our database. 1.Click "ajax_insert" database that we have created earlier. 2.Click SQL and paste the below code.
  1. INSERT INTO `post` (`post_text`, `date_time`) VALUES
  2. ('Ureka!', '2017-08-29 15:53:48'),
  3. ('I got it', '2017-08-29 16:23:59'),
  4. ('Hoooray', '2017-09-14 15:00:00'),
  5. ('Hello World!!!', '2017-09-21 20:00:00'),
  6. ('Hello Guys', '2017-09-13 08:00:00'),
  7. ('Hi', '2017-09-22 23:00:00');

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
  1. <?php
  2.  
  3. //MySQLi Procedural
  4. $conn = mysqli_connect("localhost","root","","ajax_insert");
  5. if (!$conn) {
  6.         die("Connection failed: " . mysqli_connect_error());
  7. }
  8.  
  9. ?>

Creating our Landing Page

Next, we create a landing for our sample table. We name this 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.         <title></title>
  6. </head>
  7. <body>
  8.         <h2>Sample Table</h2>
  9.         <div id = "result"></div>
  10. </body>
  11. <script src = "jquery-3.1.1.js"></script>
  12. <script type = "text/javascript">
  13.         $(document).ready(function(){
  14.                
  15.                 showPost();
  16.                
  17.                 $(document).on('click', '.delete', function(){
  18.                         var id = $(this).val();
  19.                                 $.ajax({
  20.                                         type: "POST",
  21.                                         url: "delete.php",
  22.                                         data: {
  23.                                                 id: id,
  24.                                                 delpost: 1,
  25.                                         },
  26.                                         success: function(){
  27.                                                 showPost();
  28.                                         }
  29.                                 });
  30.                        
  31.                 });
  32.         /*****  *****/
  33.         });
  34.        
  35.         function showPost(){
  36.                 $.ajax({
  37.                         url: 'show_table.php',
  38.                         type: 'POST',
  39.                         async: false,
  40.                         data:{
  41.                                 show: 1
  42.                         },
  43.                         success: function(response){
  44.                                 $('#result').html(response);
  45.                         }
  46.                 });
  47.         }
  48.        
  49. </script>
  50. </html>

Creating our Delete Code

Next step is to create our delete code. We name this as "delete.php".
  1. <?php
  2.         include ('conn.php');
  3.         if(isset($_POST['delpost'])){  
  4.        
  5.                 $id=$_POST['id'];
  6.                 mysqli_query($conn,"delete from post where postid='$id'") or die(mysqli_error());
  7.         }
  8. ?>

Creating our Show Table Code

Lastly, we create our show table code that will show our table. We name this as "show_table.php".
  1. <?php
  2.         include('conn.php');
  3.         if(isset($_POST['show'])){
  4.         ?>     
  5.                 <div>
  6.                         <table border="1">
  7.                                 <thead>
  8.                                         <th>Post Date</th>
  9.                                         <th>Post Text</th>
  10.                                         <th>Action</th>
  11.                                 </thead>
  12.                                 <tbody>
  13.                                         <?php
  14.                                         $query=mysqli_query($conn,"select * from `post` order by date_time desc") or die(mysqli_error());
  15.                                         while($row=mysqli_fetch_array($query)){
  16.                                                 ?>
  17.                                                 <tr>
  18.                                                         <td><?php echo date('M-d-Y h:i A',strtotime($row['date_time'])); ?></td>
  19.                                                         <td><?php echo $row['post_text']; ?></td>
  20.                                                         <td><button type="button" value="<?php echo $row['postid']; ?>" class="delete">Delete</button></td>
  21.                                                 </tr>
  22.                                                 <?php
  23.                                         }
  24.                                         ?>
  25.                                 </tbody>
  26.                         </table>
  27.                         <br>
  28.                 </div>
  29.         <?php
  30.         }      
  31. ?>

Comments

Works perfectly. Simple and effective.

Add new comment