Confirm before delete

梦想与她 提交于 2019-12-12 03:21:07

问题


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>

回答1:


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;
    }
}  



回答2:


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;
}


来源:https://stackoverflow.com/questions/33462771/confirm-before-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!