Confirm before delete

2019-09-04 23:58发布

Below I want to confirm before deleting a record, problem here is whether I select ok or cancel from confirmation box record gets deleted on both, secondly I am not getting redirected to my required page after deletion.

<script type="text/javascript">
function ConfirmDelete(){
if (confirm("Delete Account?")){
      window.location='mains.php';
}
else {
   // do nothing
}
}
</script>

if(isset($_POST['submit'])){
$que=$db->prepare("DELETE FROM blogs WHERE id = :id");
$que->execute(array(':id'=>$postId));
}

<form method="POST">  
<input type="submit" name="submit" value="Delete" onclick="ConfirmDelete()" />
</form>

2条回答
男人必须洒脱
2楼-- · 2019-09-05 00:28

so as basically the delete button would submit the form we can change the html code of the form to

<form action="mains.php" method="POST" onsubmit="return ConfirmDelete()">  
    <input type="submit" name="submit" value="Delete"  />
</form>

and ConfirmDelete function to

function ConfirmDelete(){
    if (confirm("Delete Account?")){
          return true;
    }
    else {
       alert('sorry');
       return false;
    }
}  
查看更多
在下西门庆
3楼-- · 2019-09-05 00:31

Typically I'd put an "action" on my form then a return false; in my javascript:

<form action="mains.php" method="POST" onclick="ConfirmDelete()">  
<input type="submit" name="submit" value="Delete" />
</form>

Then js:

function ConfirmDelete() { 
    if (confirm("Delete account?")) {
        return true;
    }
    return false;
}
查看更多
登录 后发表回答