Reputation: 65
Hi I have a working link to delete a row from my database.
<a href="?action=delete&id=<? echo $id ?>" onclick="return confirm('Are you sure you want to delete?')"><strong>Delete this Case</strong></a></td>
<?php
if($_POST['action']=="delete")
{
$id = $_POST['id'];
mysql_query("DELETE FROM rmstable2 WHERE id= '$id'");
echo("> Case # $id has been deleted as requested. To see your changes please click <a href='/martinupdate.php?id=$id'>here</a></b><br>");
}
?>
what I am looking to do is instead of having a link I want to have a button that when pressed it brings up a confirmation and then deletes the row if true.
I do not want accidental deletions.
<form>
<input type="button" value="Delete this Case" onclick="return confirm('Are you sure you want to delete?')";
<a href="?action=delete&id=<? echo $id ?>">
</form>
Upvotes: 1
Views: 22649
Reputation: 10939
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.
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);
Upvotes: 2
Reputation: 10981
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>
Upvotes: 5
Reputation: 171
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>
Upvotes: 5