What I usually do is create a delete page that shows a confirmation form if the request method is "GET" and deletes the data if the method was "POST" and the user chose the "Yes" option.
Then, in the page with the delete link, I add an onclick function (or just use the jQuery confirm plugin) that uses AJAX to post to the link, bypassing the confirmation page.
Here's the idea in pseudo code:
delete.php:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['confirm'] == 'Yes') {
delete_record($_REQUEST['id']); // From GET or POST variables
}
redirect($_POST['referer']);
}
?>
<form action="delete.php" method="post">
Are you sure?
<input type="submit" name="confirm" value="Yes">
<input type="submit" name="confirm" value="No">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
<input type="hidden" name="referer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">
</form>
Page with delete link:
<script>
function confirmDelete(link) {
if (confirm("Are you sure?")) {
doAjax(link.href, "POST"); // doAjax needs to send the "confirm" field
}
return false;
}
</script>
<a href="delete.php?id=1234" onclick="return confirmDelete(this);">Delete record</a>
answered 2009-08-06T15:27:11.607