HTTP POST 请求中的 URL 参数可以吗?

发布于 2024-11-24 20:14:54 字数 377 浏览 2 评论 0原文

我正在使用一些 HTML 表单,我只想知道即使方法属性是 POST,是否可以在操作属性中使用 URL 参数?

<form action="index.php?somefield=someval" method="post">
    <input name="anotherfield" value="anothervalue" type="text" />
    <input type="submit" />
</form>

好吧,这工作正常,我可以在回发页面中获取所有字段及其值,但我想知道这样做是否违反了某些规则、标准或其他内容?如果可以的话,请展示一些可以证明它没问题的资源,因为我在 W3.org 中找不到它。

I'm working with some HTML forms and I just want to know if it is okay to use a URL parameters in the action attribute even if the method attribute is POST?

<form action="index.php?somefield=someval" method="post">
    <input name="anotherfield" value="anothervalue" type="text" />
    <input type="submit" />
</form>

Well, this works fine, I can get all fields and their values in my postback page but I want to know if I'm breaking some rules, standard or something by doing this? Please, if you can, show some resource that can prove it is okay because I can't find it in W3.org.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱人如己 2024-12-01 20:14:54

正如规范 RFC1866 第 8.2.3 节 所述:

如果与表单处理相关的服务有侧面
效果(例如,修改数据库或订阅
服务),方法应为“POST”。

处理操作 URL 为 HTTP URL、方法为
‘POST’,用户代理使用以下命令进行 HTTP POST 事务
操作 URI,以及类型为“application/x-www-form-”的消息正文
urlencoded' 格式如上所述。用户代理应该显示
来自 HTTP POST 交互的响应,就像它显示的一样
来自上面的 HTTP GET 的响应。

发送 POST 请求时,表单数据实际上是在请求正文中发送的,而不是在标头中发送的。因此请求 URL(表单的 action)与请求正文不同。

后台发送到服务器的数据如下所示:

POST /path/script.php?somefield=somevar HTTP/1.1
User-Agent: User-Agent-String/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32

home=Cosby&favorite+flavor=flies

As the specifications RFC1866 section 8.2.3 states that:

If the service associated with the processing of a form has side
effects (for example, modification of a database or subscription to a
service), the method should be 'POST'.

To process a form whose action URL is an HTTP URL and whose method is
'POST', the user agent conducts an HTTP POST transaction using the
action URI, and a message body of type 'application/x-www-form-
urlencoded' format as above. The user agent should display the
response from the HTTP POST interaction just as it would display the
response from an HTTP GET above.

When sending a POST request, the form data is actually sent in the body of the request, not in the header. So the request URL (the form's action) is different from the request body.

The data sent to the server in the background looks like this:

POST /path/script.php?somefield=somevar HTTP/1.1
User-Agent: User-Agent-String/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 32

home=Cosby&favorite+flavor=flies
◇流星雨 2024-12-01 20:14:54

只要你的代码是一致的并且可以工作,你就应该没问题。仅在有理由的情况下才分离参数,然后将其记录下来,以便稍后出现的任何人都清楚原因。

当然,还要对其进行测试以确保其有效。它可能不是标准的,但如果你有某种原因,没有什么可以说你不能这样做。

As long as your code is consistent and works you should be fine. Only seperate the parameters if you have a reason to, and then document it so its clear why to anyone who comes along later.

And of course test it to make sure it works. Its probably not standard, but if you have some reason there's nothing to say you can't do it.

[旋木] 2024-12-01 20:14:54

它有效,但有时会令人困惑。如果您的 somefield=someval 与您的表单相关,那么最好这样做:

<form action="index.php" method="POST">
    <input name="somefield" value="someval" type="hidden" />
    <input name="anotherfield" value="anothervalue" type="text" />
    <input type="submit" />
</form>

但是如果您的 somefield=someval 与表单无关,那么您应该将其保留为 GET,这样它就不会成为表单数据的一部分。

It works, but can sometimes be confusing. If your somefield=someval is relevant to your form, it's probably better to do:

<form action="index.php" method="POST">
    <input name="somefield" value="someval" type="hidden" />
    <input name="anotherfield" value="anothervalue" type="text" />
    <input type="submit" />
</form>

But if your somefield=someval is irrelevant from the form then you should keep it as GET so it doesn't become part of your form data.

朦胧时间 2024-12-01 20:14:54

您可以使用 $_REQUEST["name"] 从 get 或 post 模式获取值。 查看 < /a>了解更多详情

you can use $_REQUEST["name"] to get values from both get or post mode. Check out for more details

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文