Friends in this video I will explain to you how to how to delete data using Checkbox in PHP with Ajax


 Index.php


<?php
include('db.php');
$res=mysqli_query($con,"select * from student");
?>
<a href="javascript:void(0)" class="link_delete" onclick="delete_all()">Delete</a>
<form method="post" id="frm">
<table id="customers">
<tr>
<th width="15%"><input type="checkbox" onclick="select_all()"  id="delete"/></th>
<th width="15%">ID</th>
<th width="70%">Name</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($res)){
?>
<tr id="box<?php echo $row['id']?>">
<td><input type="checkbox" id="<?php echo $row['id']?>" name="checkbox[]" value="<?php echo $row['id']?>"/></td>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
</tr>
<?php
}
?>
</table>
</form>
<script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  ></script>
<script>
function select_all(){
if(jQuery('#delete').prop("checked")){
jQuery('input[type=checkbox]').each(function(){
jQuery('#'+this.id).prop('checked',true);
});
}else{
jQuery('input[type=checkbox]').each(function(){
jQuery('#'+this.id).prop('checked',false);
});
}
}

function delete_all(){
var check=confirm("Are you sure?");
if(check==true){
jQuery.ajax({
url:'delete.php',
type:'post',
data:jQuery('#frm').serialize(),
success:function(result){
jQuery('input[type=checkbox]').each(function(){
if(jQuery('#'+this.id).prop("checked")){
jQuery('#box'+this.id).remove();
}
});
}
});
}
}
</script>

<style>
#customers {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 30%;
}

#customers td, #customers th {
  border: 1px solid #ddd;
  padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #4CAF50;
  color: white;
}
#frm{
margin-top:10px;
}
.link_delete{
font-size: 20px;
    color: black;
    font-family: arial;
}
</style>

database connections

<?php
$con=mysqli_connect('localhost','database_username','database_password','database_name');
?>



delete.php




<?php
include('db.php');
if(isset($_POST['checkbox'][0])){
foreach($_POST['checkbox'] as $list){
$id=mysqli_real_escape_string($con,$list);
mysqli_query($con,"delete from student where id='$id'");
}
}
?>







tables.php





-- phpMyAdmin SQL Dump
-- version 4.9.5
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Mar 05, 2021 at 08:22 AM
-- Server version: 10.4.15-MariaDB
-- PHP Version: 7.2.34

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `u559279439_test`
--

-- --------------------------------------------------------

--
-- Table structure for table `student`
--

CREATE TABLE `student` (
  `id` int(11) NOT NULL,
  `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `student`
--

INSERT INTO `student` (`id`, `name`) VALUES
(1, 'Vishal'),
(2, 'Amit'),
(3, 'Submit'),
(4, 'Namit'),
(5, 'Jatin'),
(6, 'Ram'),
(7, 'Bhaavit'),
(8, 'Sumit');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `student`
--
ALTER TABLE `student`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `student`
--
ALTER TABLE `student`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


For Best Hosting Plan Check:- https://www.hostinger.in/programmingw...​ Also, use the below Coupon Code for an attractive discount: PROGRAMMINGWITHVISHAL Learn PHP Online with Me:- https://bit.ly/LearnPHPOnline​ Source Code Link:- http://bit.ly/PHPAjaxDeleteDataUsingC...​ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ✅Subscribe: https://www.youtube.com/channel/UCWgC...​ I am a Digital Marketer by passion and a Developer by profession. With experience and expertise spanning around 10 years, in the Digital space. Good amount of experience in development of web applications using PHP, .Net, JavaScript Library’s, Frameworks, CMS, API’s, Reporting tools and Payment Gateways. Skilled in digital viral marketing, technology innovation's, brand building and all phases of the Web development lifecycle with an expert in translating business requirements into technical solutions and fanatical about quality, usability, security, and scalability. Dealing with and resolving problems and issues which arise.