JavaScript Confirm Before Delete

Before a destructive action such as deleting a record, a confirmation dialog gives the user one more chance to continue or cancel. The original Plus2net example passes a record title and id into a JavaScript function and returns the result of confirm() to the link.

Delete sample record

JavaScript Confirmation Function Top ↑

function confirmSubmit(title, id) {
  const message = `Are you sure you want to delete the data? ${title} - ID=${id}`;
  return window.confirm(message);
}

Use the Result with a Server-Side Delete Link Top ↑

The deletion itself must still be enforced on the server. JavaScript confirmation is a user-interface safeguard, not authorization or security.

<?php
$title = 'Example title';
$id = 25;
$url = 'display_k.php?id=' . urlencode((string) $id) . '&todo=delete_k';
?>
<a href="<?= htmlspecialchars($url) ?>"
   onclick="return confirmSubmit(<?= json_encode($title) ?>, <?= json_encode($id) ?>);">Delete</a>

The original page used an inline onclick handler because returning false cancels navigation. For a modern application, you can instead attach a listener and submit a form or request only after confirmation. Destructive operations should generally use POST/DELETE semantics with CSRF protection rather than a simple GET URL.

If JavaScript availability matters to a feature, see enable JavaScript and detect JavaScript support.

Displaying Popup window →

← Window Object Bookmark a page → Redirect Page →




Subscribe to our YouTube Channel here



plus2net.com







deee

28-03-2012

Thanks, this works nicely and probably saved me a bunch of time.



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer