【问题标题】:terminating the php form action on click of confirmation box单击确认框时终止 php 表单操作
【发布时间】:2012-04-15 19:38:41
【问题描述】:

我们使用这个 javascript 提醒用户,现在我想将此功能添加到我的 html 表单中,它使用 post 方法将数据发送到我的 php 表单处理页面。

现在我想要的是在用户按下取消按钮时终止操作,这意味着不应发送使用 post 方法发送的所有数据,并且该人将无法处理表单,除非他/她单击OK 按钮。那么有可能吗?或者这只是一个白痴问题?

<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button!");
if (r==true)
  {
  alert("You pressed OK!");
  }
else
  {
  alert("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show a confirm box" />

</body>
</html>

【问题讨论】:

    标签: php javascript forms onclick confirmbutton


    【解决方案1】:

    在提交表单时调用confirm,而不是在单击按钮时调用,然后将按钮更改为提交按钮:

    <form onsubmit="return show_confirm();">
        <input type="text" name="test" value="test"><br>
        <input type="submit" value="Show a confirm box">
    </form>
    

    如果点击了cancel,则添加return false;

    function show_confirm() {
        var r=confirm("Press a button!");
        if (r===true) {
            alert("You pressed OK!");
        } else {
            alert("You pressed Cancel!");
            return false;
        }
    }
    

    另见my example

    【讨论】:

      【解决方案2】:

      首先,您应该只将 ajax 调用放在 r===true 部分 然后,如果用户单击取消,您将不会向服务器发送任何数据。

      其次,如果您想终止用户已经执行的操作(=ajax 调用)(意思是,她单击“确定”按钮并调用 ajax 将数据发布到您的服务器),它将是更难,因为您将需要一个服务器端代码来执行“回滚”。您无法在数据到达服务器之前“捕获”数据,因此您的选择是能够进行回滚,然后从客户端检查状态。

      【讨论】:

        猜你喜欢
        • 2011-04-27
        • 1970-01-01
        • 2011-10-18
        • 1970-01-01
        • 1970-01-01
        • 2014-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多