Delete Button And Confirmation
Solution 1:
Try this at the top of your file:
<?phpif ($_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:
<formmethod="POST"onsubmit="return confirm('Are you sure you want to delete this case?');"><inputtype="hidden"name="_METHOD"value="DELETE"><inputtype="hidden"name="id"value="<?phpecho$id; ?>"><buttontype="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
<formonSubmit="return confirm('Are you sure you want to delete?')"><buttontype="submit"...></form>
Solution 3:
HTML:
<formid="delete-<?phpecho$id; ?>"action="?action=delete"method="post"><inputtype="hidden"name="id"value="<?phpecho$id; ?>" /><inputtype="submit"value="Delete this Case" /></form>
JS im assuming jquery for ease:
$("#delete-<?phpecho$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.
functionloaded()
{
document.getElementById("delete-<?php echo $id; ?>").addEventListener(
"submit",
function(event)
{
if(confirm("Are you sure you want to delete?"))
{
event.preventDefault();
}
returnfalse;
},
false
);
}
window.addEventListener("load", loaded, false);
Post a Comment for "Delete Button And Confirmation"