PHP Sweetalert2 with MySQL using Ajax Example




PHP Sweetalert2  with MySQL using Ajax Example

Hi friends,

In this post, we will learn PHP Sweetalert2 with MySQL using Ajax?. i explained simply step by step Integrate Sweetalert 2 In PHP & MySQL Using Ajax. This tutorial will give you simple example of SweetAlert2 Ajax Delete Rows with PHP MySQL.

I will give you simple Example of How to use sweetalert2 in php javascript.

So let's see bellow example:



$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "aatman";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {

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





include 'connection.php';


<!DOCTYPE html>

<html lang="en">


<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>PHP Sweetalert2 with MySQL using Ajax?</title>

<link rel="stylesheet" href="">

<link href="" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<link rel="stylesheet" href="" integrity="sha512-y4S4cBeErz9ykN3iwUC4kmP/Ca+zd8n8FDzlVbq5Nr73gn1VBXZhpriQ7avR+8fQLpyq4izWm0b8s6q4Vedb9w==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<script src="" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="" integrity="sha512-7x7HoEikRZhV0FAORWP+hrUzl75JW/uLHBbg2kHnPdFmScpIeHY0ieUVSacjusrKrlA/RsA2tDOBvisFmKc3xw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>


<body class="bg-dark">

<div class="container mt-5 pt-5">

<div class="card">

<div class="card-header bg-primary text-white text-center">

<h1>PHP Sweetalert2 with MySQL using Ajax?</h1>


<div class="card-body">

<table class="table table-striped table-hover">









$result = mysqli_query($conn,"SELECT * FROM users");


$id = $row["id"];

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


<tr id="<?php echo $row["id"]; ?>">

<td><?php echo $row["username"]; ?></td>

<td><?php echo $row["email"]; ?></td>

<td><?php echo $row["mobile"]; ?></td>

<td><?php echo $row["message"]; ?></td>


<button class='delete btn btn-danger' data-id='<?php echo $row["id"]; ?>' >Delete</button>











<script type="text/javascript">




$(document).on('click', '.delete', function(){

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

title: 'Are you sure?',

text: "You won't be able to revert this!",

icon: 'warning',

showCancelButton: true,

confirmButtonColor: '#3085d6',

cancelButtonColor: '#d33',

confirmButtonText: 'Yes, delete it!',

}).then((result) => {

if (result.value){


url: 'process.php',

type: 'POST',

data: {id:id},

dataType: 'json'


.done(function(response){'Deleted!', response.message, response.status);


.fail(function(){'Oops...', 'Something went wrong with ajax !', 'error');













include 'connection.php';


$sql = "DELETE FROM `users` WHERE id=$id ";

if (mysqli_query($conn, $sql)) {

echo $id;

}else {

echo "Error: " . $sql . "<br>" . mysqli_error($conn);





I hope it will help you....