Hi everyone,
I have code where a MySQL table’s row is deleted.
Before deleting I’d like a user’s confirmation whereby dialog box.
Can anyone show me how to do that?
Thanks
PHP
cannot do that. You have to do that with Javascript
.
You can but it’s not very user friendly:
delete.php
if (isset($_POST['id'])) {
if (isset($_POST['confirm']) && $_POST['confirm'] == 1) {
$pdo->prepare('DELETE FROM table WHERE id = :id LIMIT 1');
$pdo->execute(['id' => $_POST['id']]);
}
else {
echo 'Are you sure you want to delete this record?';
echo '<form action="delete.php">';
echo '<input type="hidden" name="id" value="' . $_POST['id'] . '" />';
echo '<input type="hidden" name="confirm" value="1" />';
echo '<input type="submit" value="Yes" />';
echo '</form>';
}
}
else {
echo 'You must specify an ID';
}
As spaceshiptrooper says, you are better off doing this with javascript.
2 Likes
I am intrigued as to why this is used and not checking for ‘Yes’
2 Likes
Yes, you’re right, you could put the name attribute on the button and then check for that, it doesn’t really make any difference but your approach would require less code.
3 Likes
Probably not relevant anymore, but in ye olden days (IE6) some browsers would not submit the submit button value with the form, so you had to use a hidden input like TomB did.
2 Likes
Thanks a lot !
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.