In this post I show you delete multiple records from mysql database on the basis of selected checkbox in php. You have show lots of tutorial on this things but today I have show you how to use Ajax and JQuery for delete multiple records on the basis of checked checkboxes on one single click without page refresh. When user want to remove multiple table show he can checked checkbox of that table row and click on delete button when system will ask you do you want to delete this records if he click yes then it again validate that use has checked check box or not if he not select any check box then system will again display alert message regarding please select atleast one checkbox and if user has select one or multiple checkboxes then user can remove multiple table row with background color change and fade out effect with out page refresh. For this things I have used ajax function call in jquery code. With help of Ajax it execute delete code from delete.php file. So, this is my tutorial on how to use Ajax to delete Multiple records from database with change background color of table and fade out effect without page refresh.
Source Code
index.php
<?php//index.php$connect=mysqli_connect("localhost", "root", "", "testing");
$query="SELECT * FROM tbl_customer";
$result= mysqli_query($connect, $query);
?><!DOCTYPE html><html><head><title>Webslesson Tutorial</title><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><linkrel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script><style>#box
{
width:600px;
background:gray;
color:white;
margin:0auto;
padding:10px;
text-align:center;
}
</style></head><body><divclass="container"><br/><h3align="center">Delete multiple rows by selecting checkboxes using Ajax Jquery with PHP</h3><br/><?phpif(mysqli_num_rows($result) >0)
{
?><divclass="table-responsive"><tableclass="table table-bordered"><tr><th>Customer Name</th><th>Customer Address</th><th>Delete</th></tr><?phpwhile($row= mysqli_fetch_array($result))
{
?><trid="<?phpecho$row["CustomerID"]; ?>"><td><?phpecho$row["CustomerName"]; ?></td><td><?phpecho$row["Address"]; ?></td><td><inputtype="checkbox"name="customer_id[]"class="delete_customer"value="<?phpecho$row["CustomerID"]; ?>"/></td></tr><?php
}
?></table></div><?php
}
?><divalign="center"><buttontype="button"name="btn_delete"id="btn_delete"class="btn btn-success">Delete</button></div></body></html><script>
$(document).ready(function(){
$('#btn_delete').click(function(){
if(confirm("Are you sure you want to delete this?"))
{
var id = [];
$(':checkbox:checked').each(function(i){
id[i] = $(this).val();
});
if(id.length ===0) //tell you if the array is empty
{
alert("Please Select atleast one checkbox");
}
else
{
$.ajax({
url:'delete.php',
method:'POST',
data:{id:id},
success:function()
{
for(var i=0; i<id.length; i++)
{
$('tr#'+id[i]+'').css('background-color', '#ccc');
$('tr#'+id[i]+'').fadeOut('slow');
}
}
});
}
}
else
{
returnfalse;
}
});
});
</script>
can you make a tutorial on , auto complete . example i have an id number and then , if I enter my Id number on first input field , then my other information will automatically fill those input fields without refreshing the page XD , can you help me Sir plss...
I follow ur tutorial but i have an error , When i delete it was not deleted row until i refresh my page. I copy ur code also still i get error also. And row is not fadedOut also with backgroundcolour. Please try to solve my problem. Thanks
Nice work. I'm just having issues refreshing data that has been posted using Ajax. The post is successfully being executed, but the data on the view does not get refreshed with the new data.
Instead of deleting the selected rows, I would like to INSERT to a new table in the database, say selected_customers, the rows I selected in this example. Do you have a source code/example for that? That will be much appreciated.
Great work!!!
ReplyDeletecan you make a tutorial on , auto complete . example i have an id number and then , if I enter my Id number on first input field , then my other information will automatically fill those input fields without refreshing the page XD , can you help me Sir plss...
ReplyDeleteIt not work for me :(
ReplyDeleteI not delete the data from mysql
It not working with me:(
ReplyDeleteThe data not deleting from mysql
Why if we change the name of 'Var id' and the other 'id', the success function not working?
ReplyDeleteNot work!
ReplyDeleteI follow ur tutorial but i have an error , When i delete it was not deleted row until i refresh my page. I copy ur code also still i get error also. And row is not fadedOut also with backgroundcolour. Please try to solve my problem. Thanks
ReplyDeleteHey, I had a problem with transition, could you help me ?
ReplyDeleteHey. I got problem with transition, could you help me with that issue ?
ReplyDeletecan i insert into another table with these record by selecting the value
ReplyDeleteHave you made it? I need the same.
DeleteNice work. I'm just having issues refreshing data that has been posted using Ajax. The post is successfully being executed, but the data on the view does not get refreshed with the new data.
ReplyDeleteIt's not working
ReplyDeleteworking ... but after delete row … same data not deleted in phpmyadmin page … what is reason?
ReplyDeletequery not working
DeleteInstead of deleting the selected rows, I would like to INSERT to a new table in the database, say selected_customers, the rows I selected in this example. Do you have a source code/example for that? That will be much appreciated.
ReplyDeleteit's working..thanks..
ReplyDeleteits work but how to check all
ReplyDeleteNot working!!!
ReplyDeletebad code!!! not posting data
ReplyDeleteThank you soooo much for this...really helpful!
ReplyDeleteworking ... but after delete row … same data not deleted in phpmyadmin page … what is reason? SAME PROBLEM BRO
ReplyDeleteIt worked flawlessly for me.. It deleted the data both on the table and the phpadmin. Great video and great job.
ReplyDelete