【问题标题】:HTML form still sets PHP $_POST[] despite using Onsubmit confirmation尽管使用了 Onsubmit 确认,HTML 表单仍然设置 PHP $_POST[]
【发布时间】:2015-02-19 18:45:50
【问题描述】:

HTML -

<form id="anID" method="post" onsubmit="confirm('Want to make this change?')" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
    <input type="text" name="amount">
    <input type="submit" name="add" value="Add Amount">
</form>

PHP -

if (isset($_POST['add'])) { Add amount to database }

我的问题是,尽管在出现 onsubmit 对话框时按下“取消”,但仍设置了 POST 表单,并且仍然提交了 PHP If() 语句中的所有代码,我没有想要这个。

有没有办法在isset()被激活之前确认提交?

最好是一个简单的解决方案,不需要单独的 JS 函数。

【问题讨论】:

    标签: javascript php html forms post


    【解决方案1】:

    您需要return 来自confirm() 的响应来阻止表单提交(如果它返回false。返回true 将允许提交表单):

    onsubmit="return confirm('Want to make this change?')"
    

    【讨论】:

      【解决方案2】:

      首先你还没有关闭表单语句。

      最重要的是 - 你必须设置从对话框返回到窗体。 这是正确的行:

      <form id="anID" method="post" onsubmit="return  confirm('Want to make this change?')" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      

      【讨论】:

        猜你喜欢
        • 2016-11-07
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-13
        • 1970-01-01
        • 2014-05-11
        相关资源
        最近更新 更多