首页 > 解决方案 > 在php中确认后删除

问题描述

我有一个联系表格。现在当我需要删除一条记录时,我需要一个确认框来要求用户确认。我已经做到了,到目前为止一切都很好。OK但是和Cancel按钮的记录都被删除了。

我的代码如下:

<td><a href="edit.php?id=<?php $_SESSION["name"] = ""; $_REQUEST["id"]=$row["first_name"]; echo $row["first_name"]; $_SESSION["name"]=$row["first_name"]; ?>">Edit</a> / <a href="delete.php?id=<?php echo $row["first_name"]; ?>" onclick="check()"> Delete </a></td>

<script>
    function check(){
        if(confirm("Are you sure you want to delete?")){
            window.location.href = "delete.php?id=<?php echo $row["first_name"]; ?>";
            return true;
        }
        else{
            header("Location: http://localhost/test/display.php?show_details=Show+Details");
            return false;
        }
    }
</script> 

OK只有单击确认框中的按钮并单击返回display.php(同一页面)后,我该怎么做才能删除记录Cancel

它应该delete.php仅在单击时导航到OK并停留在display.php单击时Cancel

我是 PHP 新手。请帮助...

标签: javascriptdom-events

解决方案


HTML 锚点有一个默认的点击操作:打开一个链接。现在,如果您在 JavaScript 中添加另一个点击处理程序,您将不得不阻止该元素的默认操作,从而阻止打开链接。

为了防止内联事件处理程序(与在 JSonclick中使用相比使用属性设置的事件处理程序)中的默认事件操作,addEventListener()您必须false在该事件处理程序中返回。

另请参阅:如何防止 onclick 方法中的默认事件处理?

以下两个片段修复了该问题,并且也重新使用了href-attribute,因此不得再次在 JS 中对其进行硬编码。

变体1:

<p>
    <a href="edit.php?id=123">Edit</a>
    /
    <a href="delete.php?id=123" onclick="return check(this)">Delete</a>
</p>

<script>
    function check(anchor) {
        /* Only open the delete-link, if the confirm returns TRUE */
        if (confirm('Are you sure you want to delete?')) {
            window.location.href = anchor.href;
        }

        /* Return FALSE to prevent the default link action */
        return false;
    }
</script>

变体 2:

<p>
    <a href="edit.php?id=123">Edit</a>
    /
    <!-- Return FALSE to prevent the default link action -->
    <a href="delete.php?id=123" onclick="check(this); return false;">Delete</a>
</p>

<script>
    function check(anchor) {
        /* Only open the delete-link, if the confirm returns TRUE */
        if (confirm('Are you sure you want to delete?')) {
            window.location.href = anchor.href;
        }
    }
</script>

变体 3A:

不返回 false,而是使用event.preventDefault(). (灵感来自@ths 的评论)

<p>
    <a href="edit.php?id=123">Edit</a>
    /
    <a href="delete.php?id=123" onclick="check(event, this)">Delete</a>
</p>

<script>
    function check(event, anchor) {
        /* Prevent the default link action */
        event.preventDefault();

        /* Only open the delete-link, if the confirm returns TRUE */
        if (confirm('Are you sure you want to delete?')) {
            window.location.href = anchor.href;
        }
    }
</script>

变体 3B:

不返回 false,而是使用event.preventDefault(). (灵感来自@ths 的评论)

<p>
    <a href="edit.php?id=123">Edit</a>
    /
    <a href="delete.php?id=123" onclick="event.preventDefault(); check(this);">Delete</a>
</p>

<script>
    function check(anchor) {
        /* Only open the delete-link, if the confirm returns TRUE */
        if (confirm('Are you sure you want to delete?')) {
            window.location.href = anchor.href;
        }
    }
</script>

推荐阅读