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菜单,为每个当前用户添加一个条目,这样就可以得到>,但我更喜欢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发送"
7 个解决方案
#1
35
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;
}
See: http://jsfiddle.net/SkQRN/
参见:http://jsfiddle.net/SkQRN/
#2
12
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 destructive action, 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 could use 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请求,需要创建一个
#3
11
You can make an the link perform an ajax post request when it's clicked.
您可以让链接在单击时执行ajax post请求。
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
您还可以让链接提交一个POST表单和javascript。
<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>
#4
7
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.
这样做的好处是,与获取请求相反,它不显示URL上的参数,这样更安全。
#5
5
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
#6
3
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.
首先,我要声明:我不认为将带有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在$_GET变量中存储URL params,在$_POST变量中存储表单数据。我怀疑它是非常PHP和服务器特有的,而且绝对不是一个值得依赖的东西。
#7
2
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来发布表单,那么这将会起作用。
#1
35
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;
}
See: http://jsfiddle.net/SkQRN/
参见:http://jsfiddle.net/SkQRN/
#2
12
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 destructive action, 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 could use 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请求,需要创建一个
#3
11
You can make an the link perform an ajax post request when it's clicked.
您可以让链接在单击时执行ajax post请求。
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
您还可以让链接提交一个POST表单和javascript。
<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>
#4
7
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.
这样做的好处是,与获取请求相反,它不显示URL上的参数,这样更安全。
#5
5
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
#6
3
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.
首先,我要声明:我不认为将带有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在$_GET变量中存储URL params,在$_POST变量中存储表单数据。我怀疑它是非常PHP和服务器特有的,而且绝对不是一个值得依赖的东西。
#7
2
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来发布表单,那么这将会起作用。