php - 表单提交遗漏了一个值

标签 php javascript forms google-chrome submit

如果在 Chrome v19 中通过 javascript 提交,我的 submit 按钮的值将会丢失。它适用于 IE8、IE9 和 FF12。我的以下页面显示了该问题:

<html>
<head>
<script type="text/javascript">
    function submitorder()
    {
        document.openord.TYPE.value='Submitted using javascript';
        document.openord.submit();
    }
</script>
</head>

<body>
<pre>
The contents of $_REQUEST are:
<?php var_dump($_REQUEST); ?>
</pre>

<form method="post" action="showParms.php" name="openord">
    <input type="hidden" name="TYPE" value="Submitted the regular ol' way">
    <input type="hidden" name="NAME" value="Frederick Q. Larson">
    <input type="submit" name="TASK" value="Submit with Javascript" onclick='submitorder();'>
    <input type="submit" name="TASK" value="Normal Submit">
</form>
</body>
</html>

点击“正常提交”,您将看到 TASK 的值,如下所示:

The contents of $_REQUEST are:
array(3) {
  ["TYPE"]=>
  string(29) "Submitted the regular ol' way"
  ["NAME"]=>
  string(19) "Frederick Q. Larson"
  ["TASK"]=>
  string(13) "Normal Submit"
}

“使用 Javascript 提交”按钮则不然:

The contents of $_REQUEST are:
array(2) {
  ["TYPE"]=>
  string(26) "Submitted using javascript"
  ["NAME"]=>
  string(19) "Frederick Q. Larson"
}

在 IE 和 FF 中,当单击“使用 Javascript 提交”时,您会看到:

The contents of $_REQUEST are:
array(3) {
  ["TYPE"]=>
  string(26) "Submitted using javascript"
  ["NAME"]=>
  string(19) "Frederick Q. Larson"
  ["TASK"]=>
  string(22) "Submit with Javascript"
}

知道 Chrome 为何会出现此问题吗?

最佳答案

如果您只想提交带有通过 JavaScript 更改的参数的表单,则

function submitorder() {
    document.openord.TYPE.value='Submitted using javascript';
}

就足够了,因为当用户单击提交按钮时,表单将自动提交。

“正确”行为

根据Specification ,“4.10.22.4 构建表单数据集”部分:

The algorithm to construct the form data set for a form form optionally in the context of a submitter submitter is as follows. If not specified otherwise, submitter is null.
. . .
3. Loop: For each element field in controls, in tree order, run the following substeps:
    1. If any of the following conditions are met, then skip these substeps for this element:
    . . .
    •   The field element is a button but it is not submitter.

提交表单时使用此算法 ( specification reference )。
显然,由于没有单击任何按钮,提交者为空,因此没有提交元素。

在 Firefox 中,启动提交表单功能的按钮似乎被标记为提交者。在没有(表单提交)按钮的情况下调用该函数会显示与 Chrome 中相同的行为:http://jsfiddle.net/3fwcr/1/ .

event.preventDefault(); 添加到按钮的 click 事件时,表单也会像 Chrome 中一样提交:http://jsfiddle.net/3fwcr/2/ .
显然,即使表单已经被提交,Firefox的默认提交仍然被触发(似乎违反规范:“如果表单已经被提交(..),则中止这些步骤”(“这些步骤”=表单提交))。

关于php - 表单提交遗漏了一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11090907/

相关文章:

PHP - UTF8 未正确显示,尽管一切都应该是正确的

php - 在 Codeception 套件完成后运行自定义代码

javascript - 尝试使用knockout js将不同数据绑定(bind)到同一div时显示重复记录

javascript - 用动画移动一个 div

Notepad++ 复制和粘贴到字符串上的特定位置

javascript - 下拉列表不起作用?

当插入到 mysql 时,PHP 缺少动态表中的第一行复选框值?

php - 从 mysql 中的类别部分创建列表

javascript - 用php突出显示html表中的选定行

forms - 发送 FROM 到银行时的 ssl_error_rx_record_too_long