删除前确认

时间:2015-11-01 13:42:26

标签: javascript php confirm

下面我要在删除记录之前确认,这里的问题是我是选择确定还是取消确认框记录都被删除了,其次我没有被删除后重定向到我要求的页面。

<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 个答案:

答案 0 :(得分:2)

因此基本上delete按钮会提交表单 我们可以将表单的html代码更改为

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

ConfirmDelete功能

function ConfirmDelete(){
    if (confirm("Delete Account?")){
          return true;
    }
    else {
       alert('sorry');
       return false;
    }
}  

答案 1 :(得分:0)

通常我会采取行动&#34;在我的表格上然后返回虚假;在我的javascript:

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

然后是js:

function ConfirmDelete() { 
    if (confirm("Delete account?")) {
        return true;
    }
    return false;
}