PHP MySQL confirmation box before delete record using jquery ajax




PHP MySQL confirmation box before delete record using jquery ajax

Hi Guys,

In this tutorial,I will learn you how to use confirmation alert before delete record with jquery can easy and simply use confirmation alert before delete record with jquery ajax.

Today, i will show you how to add confirm box before delete item in php. It's always necessary to confirm yes or no about that removing record. If we confirm before delete records it's safe to client that delete right records. So in this post i will show you how to add confirmation popup of jquery and remove item dynamically.

Step 1: Table structure

I am using posts table in the example and added some records.

CREATE TABLE `posts` (


`title` varchar(200) NOT NULL,

`content` text NOT NULL,

`link` varchar(255) NOT NULL


Step 2: Configuration

Create a config.php file for the database configuration.


$host = "localhost"; /* Host name */

$user = "root"; /* User */

$password = ""; /* Password */

$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);

// Check connection

if (!$con) {

die("Connection failed: " . mysqli_connect_error());


Step 3: Download & Include

Include with Bootstrap and jQuery library.

<link href='bootstrap/css/bootstrap.min.css' rel='stylesheet' type='text/css'>

<script src=""></script>

<script src='bootstrap/js/bootstrap.min.js'></script>

<script src='bootbox.min.js'></script>

Step 4: HTML & PHP

Fetch records from posts table and list in the table.


include "config.php";


<div class='container'>

<table border='1' class='table'>

<tr style='background: whitesmoke;'>






$query = "SELECT * FROM posts";

$result = mysqli_query($con,$query);

$count = 1;

while($row = mysqli_fetch_assoc($result) ){

$id = $row['id'];

$title = $row['title'];

$link = $row['link'];



<td align='center'><?= $count ?></td>

<td><a href='<?= $link ?>' target='_blank'><?= $title ?></a></td>

<td align='center'>

<button class='delete btn btn-danger' id='del_<?= $id ?>' data-id='<?= $id ?>' >Delete</button>









Step 5: PHP

Create a new ajaxfile.php file.

Check if 'id' is POST or not. If POST then prepares DELETE query to delete a record from posts table by id and return 1.


include "config.php";


$id= $_POST['id'];

$sql = "DELETE FROM posts WHERE id=".$id;


echo 1;



echo 0;


Step 6: jQuery

On the button click assign this to el variable and get delete id from data-id attribute..

Show confirmation box by calling bootbox.confirm() method.

If clicked on the OK button then send an AJAX to delete a record.


// Delete


var el = this;

// Delete id

var deleteid = $(this).data('id');

// Confirm box

bootbox.confirm("Do you really want to delete record?", function(result) {


// AJAX Request


url: 'ajaxfile.php',

type: 'POST',

data: { id:deleteid },

success: function(response){

// Removing row from HTML Table

if(response == 1){






bootbox.alert('Record not deleted.');








It will help you...