Html 提交带有查询字符串参数和隐藏参数的 GET 表单消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1116019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:13:43  来源:igfitidea点击:

submitting a GET form with query string params and hidden params disappear

htmlformssubmit

提问by Arjan

Consider this form:

考虑这种形式:

<form action="http://www.blabla.com?a=1&b=2" method="GET">
    <input type="hidden" name="c" value="3" /> 
</form>

When submitting this form (a GET form) the parameters a and b are disappearing. Is there a reason for that? Is there a way of avoiding this behaviour?

提交此表单(GET 表单)时,参数 a 和 b 将消失。有什么原因吗?有没有办法避免这种行为?

回答by Arjan

Isn't that what hidden parameters are for to start with...?

这不是隐藏参数的开始......?

<form action="http://www.example.com" method="GET">
  <input type="hidden" name="a" value="1" /> 
  <input type="hidden" name="b" value="2" /> 
  <input type="hidden" name="c" value="3" /> 
  <input type="submit" /> 
</form>

I wouldn't count on any browser retaining any existing query string in the action URL.

我不会指望任何浏览器在操作 URL 中保留任何现有的查询字符串。

As the specifications (RFC1866, page 46; HTML 4.xsection 17.13.3) state:

正如规范(RFC1866,第 46 页;HTML 4.x第 17.13.3 节)所述:

If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.

如果方法是“get”,而动作是一个 HTTP URI,用户代理会接受动作的值,并附加一个“?” 到它,然后附加表单数据集,使用“application/x-www-form-urlencoded”内容类型进行编码。

Maybe one could percent-encode the action-URL to embed the question mark and the parameters, and then cross one's fingers to hope all browsers would leave that URL as it (and validate that the server understands it too). But I'd never rely on that.

也许可以对 action-URL 进行百分比编码以嵌入问号和参数,然后交叉手指希望所有浏览器都保留该 URL(并验证服务器是否也理解它)。但我永远不会依赖它。

By the way: it's not different for non-hidden form fields. For POST the action URL could hold a query string though.

顺便说一句:对于非隐藏的表单字段,它没有什么不同。对于 POST,动作 URL 可以包含一个查询字符串。

回答by xyphoid

In HTML5, this is per-spec behaviour.

在 HTML5 中,这是每个规范的行为。

See http://www.w3.org/TR/2011/WD-html5-20110525/association-of-controls-and-forms.html#form-submission-algorithm

http://www.w3.org/TR/2011/WD-html5-20110525/association-of-controls-and-forms.html#form-submission-algorithm

Look at "4.10.22.3 Form submission algorithm", step 17. In the case of a GET form to an http/s URI with a query string:

查看“4.10.22.3 表单提交算法”,第 17 步。对于带有查询字符串的 http/s URI 的 GET 表单:

Let destination be a new URL that is equal to the action except that its <query>component is replaced by query (adding a U+003F QUESTION MARK character (?) if appropriate).

令destination 是一个新的URL,它与action 相同,只是它的<query>组件被query 替换(如果合适,添加一个U+003F 问题标记字符(?))。

So, your browser will trash the existing "?..." part of your URI and replace it with a new one based on your form.

因此,您的浏览器将删除 URI 中现有的“?...”部分,并根据您的表单将其替换为新的。

In HTML 4.01, the spec produces invalid URIs - most browsers didn't actually do this though..

在 HTML 4.01 中,规范产生了无效的 URI——尽管大多数浏览器实际上并没有这样做。

See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3, step four - the URI will have a ? appended, even if it already contains one.

参见http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3,第四步 - URI 将有一个 ? 附加,即使它已经包含一个。

回答by Efx

What you can do is using a simple foreach on the table containing the GET information. For example in php :

您可以做的是在包含 GET 信息的表上使用一个简单的 foreach。例如在 php 中:

foreach ($_GET as $key => $value) {
    echo("<input type='hidden' name='$key' value='$value'/>");
}

回答by Bernhard Hofmann

You should include the two items (a and b) as hidden input elements as well as C.

您应该包括两个项目(a 和 b)作为隐藏的输入元素以及 C。

回答by KillerRabbit

I had a very similar problem where for the form action, I had something like:

我有一个非常相似的问题,对于表单操作,我有类似的问题:

<form action="http://www.example.com/?q=content/something" method="GET">
   <input type="submit" value="Go away..." />&nbsp;
</form>

The button would get the user to the site, but the query info disappeared so the user landed on the home page rather than the desired content page. The solution in my case was to find out how to code the URL without the query that would get the user to the desired page. In this case my target was a Drupal site, so as it turned out /content/somethingalso worked. I also could have used a node number (i.e. /node/123).

该按钮会将用户带到该站点,但查询信息消失了,因此用户登陆的是主页而不是所需的内容页面。在我的案例中,解决方案是找出如何在没有查询的情况下对 URL 进行编码,从而使用户访问所需的页面。在这种情况下,我的目标是一个 Drupal 站点,所以结果证明它/content/something也有效。我也可以使用节点号(即/node/123)。

回答by wanis

If you need workaround, as this form can be placed in 3rd party systems, you can use Apache mod_rewrite like this:

如果您需要解决方法,因为此表单可以放置在 3rd 方系统中,您可以像这样使用 Apache mod_rewrite:

RewriteRule ^dummy.link$ index.php?a=1&b=2 [QSA,L]

then your new form will look like this:

那么您的新表单将如下所示:

<form ... action="http:/www.blabla.com/dummy.link" method="GET">
<input type="hidden" name="c" value="3" /> 
</form>

and Apache will append 3rd parameter to query

和 Apache 将附加第三个参数来查询

回答by Jay

Your construction is illegal. You cannot include parameters in the action value of a form. What happens if you try this is going to depend on quirks of the browser. I wouldn't be surprised if it worked with one browser and not another. Even if it appeared to work, I would not rely on it, because the next version of the browser might change the behavior.

你的建筑是非法的。您不能在表单的操作值中包含参数。如果您尝试这样做会发生什么取决于浏览器的怪癖。如果它适用于一种浏览器而不是另一种浏览器,我不会感到惊讶。即使它看起来有效,我也不会依赖它,因为浏览器的下一个版本可能会改变行为。

"But lets say I have parameters in query string and in hidden inputs, what can I do?" What you can do is fix the error. Not to be snide, but this is a little like asking, "But lets say my URL uses percent signs instead of slashes, what can I do?" The only possible answer is, you can fix the URL.

“但是假设我在查询字符串和隐藏输入中有参数,我该怎么办?” 您可以做的是修复错误。不是讽刺,但这有点像问,“但假设我的 URL 使用百分号而不是斜杠,我该怎么办?” 唯一可能的答案是,您可以修复 URL。

回答by TH_

This is in response to the above post by Efx:

这是对 Efx 的上述帖子的回应:

If the URL already contains the var you want to change, then it is added yet again as a hidden field.

如果 URL 已包含您要更改的变量,则会再次将其添加为隐藏字段。

Here is a modification of that code as to prevent duplicating vars in the URL:

以下是对该代码的修改,以防止在 URL 中重复变量:

foreach ($_GET as $key => $value) {
    if ($key != "my_key") {
        echo("<input type='hidden' name='$key' value='$value'/>");
    }
}

回答by Shashidhar Gr

<form ... action="http:/www.blabla.com?a=1&b=2" method ="POST">
<input type="hidden" name="c" value="3" /> 
</form>

change the request method to' POST' instead of 'GET'.

将请求方法更改为“POST”而不是“GET”。

回答by Rápli András

I usually write something like this:

我通常这样写:

foreach($_GET as $key=>$content){
        echo "<input type='hidden' name='$key' value='$content'/>";
}

This is working, but don't forget to sanitize your inputs against XSS attacks!

这是有效的,但不要忘记清理您的输入以防止 XSS 攻击!