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,
- Add/Remove Selected Element from Multi Select
- Add/Delete Record Dynamically Using jQuery
- How To Delete Multiple Selected Rows Using Ajax
- How to find selected radio in a radio group list
We’ll implement the following Functionality
- Enabling users to check all or uncheck all checkboxes using jQuery.
- Deleting a single row from a table using either PHP on the server-side or jQuery on the client-side.
- Deleting multiple rows from a table with empty validation, employing either PHP on the server-side or jQuery on the client-side.
Simple jQuery Script to Delete Single/Multiple Selected Rows
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(); }); } } });
JavaScript Function to Delete Single Record
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(); } });
Conclusion:
in this tutorial, You have learned the following functionality:
- Initialization: Start by including jQuery and Bootstrap files for the necessary functionalities.
- HTML Table: Define the structure of the HTML table with checkboxes to select rows and a delete button to initiate the deletion process.
- Selecting Rows: Implement a function to select all table records, allowing users to easily check or uncheck all elements.
- Deleting Records: Define a jQuery function to delete selected rows either via Ajax request (server-side) or client-side manipulation. Users are prompted for confirmation before deletion.
- Deleting Single Record: Implement a jQuery script to delete single records from the HTML table upon user confirmation.
Demo & Download Source Code
Please feel free to send queries to me using the comment section.
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