php 如何在 URL 中传递 POST 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6210900/
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
How to pass POST parameters in a URL?
提问by Mawg says reinstate Monica
Basically, I think that I can't, but would be very happy to be proven wrong.
基本上,我认为我不能,但会很高兴被证明是错误的。
I am generating an HTML menu dynamically in PHP, adding one item for each current user, so that I get something like <a href="process_user.php?user=<user>>
, but I have a preference for POST over GET.
我在 PHP 中动态生成一个 HTML 菜单,为每个当前用户添加一个项目,以便我得到类似的东西<a href="process_user.php?user=<user>>
,但我更喜欢 POST 而不是 GET。
Is there any way to pass the info as a POST parameter, rather than GET from a clickable HREF link?
有什么方法可以将信息作为 POST 参数传递,而不是从可点击的 HREF 链接中获取?
Update: sorry, I am not allowed to use JS - I shoulda said, my bad
更新:对不起,我不允许使用 JS - 我应该说,我的不好
Update to the update: it looks like @Rob is on to somethign with "You could use a button instead of an anchor and just style the button to look like a link. That way you could have your values in hidden fields inside the same form to be sent via POST"
更新更新:看起来@Rob 正在使用“您可以使用按钮而不是锚点,并将按钮的样式设置为看起来像链接。这样您就可以在同一表单内的隐藏字段中设置值通过 POST 发送”
回答by Petah
You could use a form styled as a link, no JavaScript required:
您可以使用样式为链接的表单,不需要 JavaScript:
<form action="/do/stuff.php" method="post">
<input type="hidden" name="user_id" value="123" />
<button>Go to user 123</button>
</form>
CSS:
CSS:
button {
border: 0;
padding: 0;
display: inline;
background: none;
text-decoration: underline;
color: blue;
}
button:hover {
cursor: pointer;
}
回答by deceze
Parameters in the URL are GET parameters, a request body, if present, is POST data. So your basic premise is by definition not achievable.
URL 中的参数是 GET 参数,请求正文(如果存在)是 POST 数据。因此,根据定义,您的基本前提是无法实现的。
You should choose whether to use POST or GET based on the action. Any destructiveaction, i.e. something that permanently changes the state of the server (deleting, adding, editing) should always be invoked by POST requests. Any pure "information retrieval" should be accessible via an unchanging URL (i.e. GET requests).
您应该根据操作选择是使用 POST 还是 GET。任何破坏性操作,即永久更改服务器状态(删除、添加、编辑)的操作都应始终由 POST 请求调用。任何纯粹的“信息检索”都应该可以通过不变的 URL(即 GET 请求)访问。
To make a POST request, you need to create a <form>
. You coulduse Javascript to create a POST request instead, but I wouldn't recommend using Javascript for something so basic. If you want your submit button to look like a link, I'd suggest you create a normal form with a normal submit button, then use CSS to restyle the button and/or use Javascript to replace the button with a link that submits the form using Javascript (depending on what reproduces the desired behavior better). That'd be a good example of progressive enhancement.
要发出 POST 请求,您需要创建一个<form>
. 您可以使用 Javascript 来创建 POST 请求,但我不建议将 Javascript 用于如此基本的事情。如果您希望提交按钮看起来像一个链接,我建议您创建一个带有普通提交按钮的普通表单,然后使用 CSS 重新设置按钮样式和/或使用 Javascript 将按钮替换为提交表单的链接使用 Javascript(取决于更好地再现所需行为的内容)。那将是渐进增强的一个很好的例子。
回答by Rob
You can make an the link perform an ajax post request when it's clicked.
您可以使链接在单击时执行 ajax 发布请求。
In jQuery:
在 jQuery 中:
$('a').click(function(e) {
var $this = $(this);
e.preventDefault();
$.post('url', {'user': 'something', 'foo': 'bar'}, function() {
window.location = $this.attr('href');
});
});
You could also make the link submit a POST form with javascript
您还可以使用 javascript 使链接提交 POST 表单
<form action="url" method="post">
<input type="hidden" name="user" value="soemthing" />
<a href="#">CLick</a>
</form>
<script>
$('a').click(function(e) {
e.preventDefault();
$(this).parents('form').submit();
});
</script>
回答by Aris
I would like to share my implementation as well. It does require some javascript though.
我也想分享我的实现。不过,它确实需要一些 javascript。
<form action="./index.php" id="homePage" method="post" style="display: none;">
<input type="hidden" name="action" value="homePage" />
</form>
<a href="javascript:;" onclick="javascript:
document.getElementById('homePage').submit()">Home</a>
Nice thing about this, is that contrary to GET requests, it doesn't show the parameters on the URL, which is safer.
这样做的好处是,与 GET 请求相反,它不显示 URL 上的参数,这更安全。
回答by raiks
First off, a disclaimer: I don't think marrying POST with URL parameters is a brilliant idea. Like others suggested, you're better off using a hidden form for passing user information.
首先,免责声明:我不认为将 POST 与 URL 参数结合是一个绝妙的主意。就像其他人建议的那样,最好使用隐藏表单来传递用户信息。
However, a question made me curious how PHP is handling such a case. It turned out that it's possible in theory. Here's a proof:
然而,一个问题让我很好奇 PHP 是如何处理这种情况的。事实证明,这在理论上是可能的。这是一个证明:
post_url_params.html
post_url_params.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<form method="post" action="post_url_params.php?key1=value1">
<input type="hidden" name="key2" value="value2">
<input type="hidden" name="key3" value="value3">
<input type="submit" value="click me">
</form>
</body>
</html>
post_url_params.php
post_url_params.php
<?php
print_r($_POST);
print_r($_GET);
echo $_SERVER['REQUEST_METHOD'];
?>
Output
输出
Array ( [key2] => value2 [key3] => value3 )
Array ( [key1] => value1 )
POST
One can clearly see that PHP stores URL params in the $_GET variable and form data in the $_POST variable. I suspect it's very PHP- and server-specific, though, and is definitely not a thing to rely on.
可以清楚地看到,PHP 将 URL 参数存储在 $_GET 变量中,并将表单数据存储在 $_POST 变量中。不过,我怀疑它是非常特定于 PHP 和服务器的,绝对不是一个可以依赖的东西。
回答by zzarbi
No you cannot do that. I invite you to read a POST definition: http://en.wikipedia.org/wiki/POST_%28HTTP%29
不,你不能那样做。我邀请您阅读 POST 定义:http: //en.wikipedia.org/wiki/POST_%28HTTP%29
or this page: http://en.wikipedia.org/wiki/GET_%28HTTP%29#Request_methods
或此页面:http: //en.wikipedia.org/wiki/GET_%28HTTP%29#Request_methods
回答by tofutim
This could work if the php script generates a form for each entry with hidden fields and the href uses javascript to post the form.
如果 php 脚本为每个带有隐藏字段的条目生成一个表单,并且 href 使用 javascript 发布表单,这可能会起作用。