How add confirmation box in php before deleting?

How add confirmation box in php before deleting?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The PHP-only Illusion: Why You Can't Confirm Deletes Without JavaScript

You cannot add a confirmation box to a delete action using PHP only. This is a fundamental misunderstanding of how server-side languages function versus user interaction.

PHP runs on the server. It processes requests, queries databases, and returns HTML. Once PHP finishes sending its response to the browser, the connection for that specific request is closed (or waits for the next one). At that point, you cannot "pop up" a dialog box or intercept a click event because the user has already received the page content.

If your goal is to prevent accidental deletions, you need client-side validation using JavaScript. While you can make the initial request via PHP, the confirmation mechanism requires the browser to interact with the user after the page has loaded.

Below are the three standard approaches to handling this, ranging from a native browser prompt to a custom modal, all requiring HTML and JavaScript.

The Native confirm() Prompt

The simplest solution relies on the browser's built-in window.confirm() API. This displays a modal dialog asking the user if they are sure. If the user clicks "OK," it returns true; otherwise, it returns false.

You can attach this directly to your link in List.php:

echo "<td><a href='delete.php?id=" . $query2['id'] . "&confirmed=true' onclick=\"if(!confirm('Are you sure you want to delete this record?')) return false;\">x</a></td>";

How This Works

  1. The user clicks the link.
  2. The browser intercepts the navigation attempt.
  3. window.confirm() pauses execution and shows a dialog.
  4. If the user cancels, the return false prevents the page from reloading.
  5. If they confirm, the page loads delete.php.

The Trade-off

The native prompt is ugly and non-customizable. It looks like a standard browser alert, which can be distracting on modern websites. More importantly, it blocks the user flow entirely until they respond. For better UX, developers often prefer hiding the form submission until confirmation occurs.

Custom Modals with AJAX

A more professional approach involves creating a custom modal overlay and using JavaScript to handle the click event without immediately navigating away from the list page. This keeps the user context intact.

In List.php, you would change the delete link to trigger a JavaScript function:

<td>
    <a href="#" class="delete-link" data-id="{$query2['id']}">x</a>
</td>

Then, include a script block (whether inline or external) that listens for clicks on these links:

document.querySelectorAll('.delete-link').forEach(function(link) {
    link.addEventListener('click', function(e) {
        e.preventDefault(); // Stop immediate navigation
        
        var id = this.getAttribute('data-id');
        
        if (!confirm('Are you sure you want to delete this record?')) {
            return; // User cancelled, do nothing
        }

        // Proceed with the deletion via AJAX or redirect
        window.location.href = 'delete.php?id=' + id;
    });
});

Why This Is Better

This method separates concerns. The HTML remains clean, and the logic for "what happens on click" is isolated in JavaScript. It also allows you to style the confirmation using CSS rather than relying on the browser's default dialog.

Server-Side Safety: The confirmed Parameter

Regardless of whether you use a native prompt or a custom modal, it is best practice to verify the user's intent on the server side before actually altering data. This prevents accidental deletions if JavaScript is disabled or if a link is bookmarked directly.

Modify delete.php to include a check:

include('config.php');

// Check if we are coming from a confirmed action
if (isset($_GET['confirmed']) && $_GET['confirmed'] === 'true') {
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        
        // Perform the deletion
        $query1 = mysql_query("delete from addd where id='$id'");
        
        if ($query1) {
            header('location:list.php');
            exit();
        }
    }
}

// If not confirmed, show an error or redirect back safely
header('location:list.php?error=deletion_cancelled');

Critical Security Note

The source code provided uses mysql_query and direct string concatenation ($id='$id'). This is vulnerable to SQL Injection attacks. You should never trust user input (even $_GET) directly in a query. Switch to prepared statements immediately:

  • Legacy MySQL: Use mysql_real_escape_string() or migrate to mysqli with prepared statements.
  • Modern PHP: Use PDO or MySQLi with bound parameters.

Using raw strings like "delete from addd where id='$id'" allows attackers to inject malicious SQL commands, potentially exposing sensitive data or deleting your entire database.

Our network