How to delete multiple selected records using PHP, jQuery, and AJAX

To delete multiple selected records using PHP, jQuery, and AJAX, you'll need to create a user interface where users can select multiple records and then trigger a deletion process. Here's a step-by-step guide on how to achieve this:

Step 1: Create the HTML Structure

Start by creating an HTML structure that displays the records you want to delete and provides checkboxes to select them. You can use a table for this purpose. For example:

<!DOCTYPE html>
    <title>Delete Multiple Records</title>
    <script src=""></script>
    <h2>Delete Multiple Records</h2>
                <!-- Add more columns as needed -->
            <!-- Generate rows with data from your database -->
                <td><input type="checkbox" class="record-checkbox"></td>
                <td>Record 1</td>
                <td><input type="checkbox" class="record-checkbox"></td>
                <td>Record 2</td>
            <!-- Add more rows as needed -->
    <button id="delete-selected">Delete Selected</button>

Step 2: Create the jQuery and AJAX Functionality

Write jQuery code to handle the selection of records and perform the deletion via AJAX. This code should capture the IDs of selected records and send them to a PHP script for deletion. Add this code within a <script> tag or in an external JavaScript file:

$(document).ready(function() {
    // Handler for the "Delete Selected" button click
    $("#delete-selected").click(function() {
        var selectedIds = [];

        // Iterate through the checkboxes to find selected records
        $(".record-checkbox:checked").each(function() {
            var recordId = $(this).closest("tr").find("td:eq(1)").text(); // Assuming the ID is in the second column

        if (selectedIds.length === 0) {
            alert("Please select at least one record to delete.");
        } else {
            // Send the selected IDs to the server for deletion
                type: "POST",
                url: "delete_records.php", // PHP script for record deletion
                data: { selectedIds: selectedIds },
                success: function(response) {
                    // Handle the server's response (e.g., display a success message)
                    // Refresh or update the record list as needed
                error: function(xhr, status, error) {
                    // Handle AJAX errors (e.g., display an error message)

Step 3: Create the PHP Script for Record Deletion (delete_records.php)

Create a PHP script (delete_records.php) that receives the selected record IDs and performs the deletion. This script should interact with your database to delete the selected records. Here's an example:

if (isset($_POST['selectedIds'])) {
    $selectedIds = $_POST['selectedIds'];

    // Connect to your database
    $servername = "localhost";
    $username = "your_username";
    $password = "your_password";
    $dbname = "your_database";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);

    // Build and execute the DELETE query
    $idList = implode(",", $selectedIds);
    $sql = "DELETE FROM your_table WHERE id IN ($idList)";

    if ($conn->query($sql) === TRUE) {
        echo "Records deleted successfully.";
    } else {
        echo "Error deleting records: " . $conn->error;

} else {
    echo "No records selected for deletion.";

Replace your_username, your_password, your_database, your_table, and the database connection details with your actual values.

Step 4: Style and Enhance

Style your HTML elements, display success/error messages, and handle any additional features or requirements based on your project needs.

With these steps, you've created a system to delete multiple selected records using PHP, jQuery, and AJAX. Users can select multiple records, click the "Delete Selected" button, and the selected records will be deleted from the database.

Muhammad Alfaiz
Alfaiz 3 months ago
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x