In this jQuery tutorial, I will let you know how to delete multiple records from the table using jQuery with Ajax, You’ll explore options to select all or uncheck all records without reloading the page.
This is a very common functionality of any HTML table listing and, nobody wants to reload the page after the delete record. The methods below are used to delete selected rows from the grid with the help of Ajax.
Check out other tutorials on jQuery,
Step 1: We have included the jQuery and bootstrap files.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
Step 2: Let’s define HTML table rows.
<div class="container" style="padding:0 250px;"> <h1>A Simple Example Delete Multile Rows Using PHP and jQuery</h1> <div class="row well"> <a type="button" class="btn btn-primary pull-right delete_all">Delete Selected</a> </div> <table id="employee_grid" class="table table-condensed table-hover table-striped bootgrid-table" width="60%" cellspacing="0"> <thead> <tr> <th><input type="checkbox" id="master"></th> <th>Name</th> <th>Salary</th> <th>Age</th> <th class="pull-right">Delete</th> </tr> </thead> <tbody> <tr data-row-id="1"> <td><input type="checkbox" class="sub_chk" data-id="1"></td> <td>Tiger Nixon</td> <td>320800</td> <td>61</td> <td><a class="remove-row pull-right" targetDiv="" data-id="1" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td> </tr> <tr data-row-id="5"> <td><input type="checkbox" class="sub_chk" data-id="5"></td> <td>Airi Satou</td> <td>162700</td> <td>33</td> <td><a class="remove-row pull-right" targetDiv="" data-id="5" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td> </tr> <tr data-row-id="6"> <td><input type="checkbox" class="sub_chk" data-id="6"></td> <td>Brielle Williamson</td> <td>372000</td> <td>61</td> <td><a class="remove-row pull-right" targetDiv="" data-id="6" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td> </tr> <tr data-row-id="7"> <td><input type="checkbox" class="sub_chk" data-id="7"></td> <td>Herrod Chandler</td> <td>137500</td> <td>59</td> <td><a class="remove-row pull-right" targetDiv="" data-id="7" href="javascript: void(0)"><i class="glyphicon glyphicon-trash"></i></a></td> </tr> </tbody> </table> </div>
Step 3: This function is used for selecting all table records. You can use this function to Check all & Uncheck all elements.
jQuery('#master').on('click', function(e) { if($(this).is(':checked',true)) { $(".sub_chk").prop('checked', true); } else { $(".sub_chk").prop('checked',false); } });
Step 4: Define the jQuery function to delete all records using Ajax
request(Server-Side) or jQuery(Client-Side).
jQuery('.delete_all').on('click', function(e) { var allVals = []; $(".sub_chk:checked").each(function() { allVals.push($(this).attr('data-id')); }); //alert(allVals.length); return false; if(allVals.length <=0) { alert("Please select row."); } else { //$("#loading").show(); WRN_PROFILE_DELETE = "Are you sure you want to delete this row?"; var check = confirm(WRN_PROFILE_DELETE); if(check == true){ //for server side /* var join_selected_values = allVals.join(","); $.ajax({ type: "POST", url: "delete.php", cache:false, data: 'ids='+join_selected_values, success: function(response) { $("#loading").hide(); $("#msgdiv").html(response); //referesh table } });*/ //for client side $.each(allVals, function( index, value ) { $('table tr').filter("[data-row-id='" + value + "']").remove(); }); } } });
Step 5: Below jQuery script is used to delete single records from the HTML table.
jQuery('.remove-row').on('click', function(e) { WRN_PROFILE_DELETE = "Are you sure you want to delete this row?"; var check = confirm(WRN_PROFILE_DELETE); if(check == true){ $('table tr').filter("[data-row-id='" + $(this).attr('data-id') + "']").remove(); } });
in this tutorial, You have learned the following functionality:
Please feel free to send queries to me using the comment section.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
how i get each id from array id in delete.php
you will get is selected rows data as ids=1,2,3,5, so can explode this string and passed to mysql delete query
tks you!!!.i have found explode function in google
As i can see in code, There is PHP code which is commented to delete rows from Mysql Using ajax
you can share or like the tutorial, that help to unlock source code link
Thank you