Delete Button And Confirmation
Solution 1:
Try this at the top of your file:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'DELETE' || ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['_METHOD'] == 'DELETE')) {
$id = (int) $_POST['id'];
$result = mysql_query('DELETE FROM rmstable2 WHERE id='.$id);
if ($result !== false) {
// there's no way to return a 200 response and show a different resource, so redirect instead. 303 means "see other page" and does not indicate that the resource has moved.
header('Location: http://fully-qualified-url/martinupdate.php?id='.$id, true, 303);
exit;
}
}
With this as the form:
<form method="POST" onsubmit="return confirm('Are you sure you want to delete this case?');">
<input type="hidden" name="_METHOD" value="DELETE">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<button type="submit">Delete Case</button>
</form>
Solution 2:
you have to put your confirmation in the onSubmit event of the form
so if the user cancel the confirmation, the form won't be sent
<form onSubmit="return confirm('Are you sure you want to delete?')">
<button type="submit" ...>
</form>
Solution 3:
HTML:
<form id="delete-<?php echo $id; ?>" action="?action=delete" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<input type="submit" value="Delete this Case" />
</form>
JS im assuming jquery for ease:
$("#delete-<?php echo $id; ?>").submit(function() {
return confirm("Are you sure you want to delete?");
});
What this does is prevent the default submit action if the js confirm returns false (doesn't submit) otherwise lets the regular post go through.
Note: you really shouldn't use html attributes to declare event handlers, this code separates the logic.
EDIT: @Nicholas comment
This is a non-jquery solution. I didn't test it, and i don't believe that preventDefault works in IE <= 8 so I probably wouldn't use it in production BUT it could be done w/o too much code jquery just makes it cross browser and easier.
function loaded()
{
document.getElementById("delete-<?php echo $id; ?>").addEventListener(
"submit",
function(event)
{
if(confirm("Are you sure you want to delete?"))
{
event.preventDefault();
}
return false;
},
false
);
}
window.addEventListener("load", loaded, false);
Post a Comment for "Delete Button And Confirmation"