php 如何通过链接传递POST变量到自己的页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6887080/
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 variable by links to own pages?
提问by Bijak Antusias Sufi
Hi i wannna get variable $_POST by link to self pages. Example :
嗨,我想通过链接到自己的页面来获取变量 $_POST。例子 :
<?PHP
$var = 'PIG';
echo "<a href='test.php?var=$var'>link</a>";
if (isset($_POST['var']))
{
echo $_POST['var']);
}
?>
it links to own pages. (test.php) It not works, who can help me please. Thanks
它链接到自己的页面。(test.php) 它不起作用,请谁能帮助我。谢谢
回答by Yanick Rochon
A link cannot POST
data, only GET
.
链接不能POST
数据,只能GET
。
In contrast to the GET request method where only a URL and headers are sent to the server, POST requests also include a message body. This allows for arbitrary length data of any type to be sent to the server.
与仅向服务器发送 URL 和标头的 GET 请求方法相比,POST 请求还包括消息正文。这允许将任何类型的任意长度数据发送到服务器。
Basically, a POST
requires two requests, 1) the server receives the "normal" request, with an extra header value indicating that more data needs to be sent. At that point, the server sends an acknowledge and 2) the client sends the POST
body. This behavior cannot be achieved only with a link.
基本上,aPOST
需要两个请求,1) 服务器收到“正常”请求,带有一个额外的标头值,表明需要发送更多数据。此时,服务器发送确认,2) 客户端发送POST
正文。仅通过链接无法实现此行为。
However, there are solutions to this and I have seen some technique, among others, outputting a form
with an autosubmit, something like
但是,对此有一些解决方案,并且我已经看到了一些技术,其中包括输出form
带有自动提交的 a,例如
<form name="frm" method="post" action="http://your.domain.com/path/to/page.php?param1=1¶m2=2">
<input type="hidden" name="foo" value="bar" />
</form>
<script type="text/javascript">
document.forms["frm"].submit();
</script>
which would result into calling page.php
with these arguments
这将导致page.php
使用这些参数进行调用
$_GET = array('param1' => '1', 'param2' => '2');
$_POST = array('foo' => 'bar');
Note that this is a simple "redirect" method, but you can create <a>
elements to actually trigger some hidden form like that instead of using the standard link. (untested code)
请注意,这是一个简单的“重定向”方法,但您可以创建<a>
元素来实际触发一些类似的隐藏表单,而不是使用标准链接。(未经测试的代码)
<a href="http://your.domain.com/path/to/page.php?param1=1¶m2=2" onclick="return dopost(this.href, {foo:'bar'});">A simple link</a>
<script type="text/javascript">
function dopost(url, params) {
var pe = '';
for (var param : params) {
pe += '<input type="hidden" name="'+param+'" value="'+params[param]+'" />';
}
var frmName = "frm" + new Date().getTime();
var form = '<form name="'+frmName+'" method="post" action="'+url'">'+pe+'</form>';
var wrapper = document.createElement("div");
wrapper.innerHTML = form;
document.body.appendChild(wrapper);
document.forms[frmName].submit();
}
</script>
This is probably what you need, actually.
实际上,这可能就是您所需要的。
回答by cwallenpoole
Unfortunately, you really can't do that. If you need to use an anchor to submit a value, then you will need to access the variables through $_GET
or $_REQUEST
.
不幸的是,你真的不能那样做。如果需要使用锚点提交值,则需要通过$_GET
或访问变量$_REQUEST
。
If it has to be a $_POST
(if you are set in that design decision, because $_GET
actually makes a lot more sense there), you can use a form and the style the submit button to make it look very much like a link. Put this code in a text editor and check it out.
如果它必须是一个$_POST
(如果你在那个设计决定中设置,因为$_GET
实际上在那里更有意义),你可以使用表单和提交按钮的样式使它看起来非常像一个链接。将此代码放入文本编辑器并检查出来。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
.button {border:none;background-color:#FFFFFF}
.button:hover{ color:blue; }
</style>
</head>
<body>
<form action="test.php">
<input type="hidden" name="var" value="<?php echo $val; ?>" />
This kinda looks like a link:
<input type="submit" value="link" class="button" />
</form>
</body>
</html>
回答by T30
If you have multiple links and you don't want to rewrite all of them, just add one fakeform like this:
如果您有多个链接并且不想重写所有链接,只需添加一个这样的假表单:
<form id="fakeForm" method="post">
<input type="hidden" name="post_key" value="post_value" />
</form>
and set up proper jquery:
并设置适当的jquery:
$('a').click(function(event){
event.preventDefault();
$('#fakeForm').attr('action',$(this).attr('href')).submit();
});
In this case, when you click on anylink, the landing page receives the post_value
variable.
在这种情况下,当您单击任何链接时,登录页面会收到该post_value
变量。
Note that if the link is clicked with other than left click (or js is disabled), the link works properly, but the value isn't passed!
请注意,如果单击链接时不是左键单击(或禁用了 js),则链接可以正常工作,但不会传递值!
回答by rockhammer
This code below demonstrates T30's idea works.
下面的代码演示了 T30 的想法。
My rationale for passing via $_POST is to prevent certain variables from being exposed in the url which is accomplished here. However, they would still be exposed via "view source".
我通过 $_POST 传递的基本原理是防止某些变量在此处完成的 url 中公开。但是,它们仍然会通过“查看源代码”公开。
<?php
/*
This demonstrates how to set $_POST from a link in .php without ajax based on the idea from http://stackoverflow.com/a/27621672/1827488. The rationale for doing so is to prevent certain variables ('userid') from being exposed in the url via $_GET. However, there does not seem to be a way to avoid those variables being exposed by 'view source'.
*/
echo "<!DOCTYPE html><html lang='en'><head><title>Test Data Link</title></head><body>";
// only one hidden form
echo "<form class='hiddenForm' method='post'>
<input class='hiddenFormUserid' type='hidden' name='userid'/>
</form>";
// as many links as you need
echo "<p><a class='hiddenFormLink' href='?following=1' data-userid=101>Following</a> • <a class='hiddenFormLink' href='?followers=1' data-userid=101>Followers</a></p>";
echo "<p><a class='hiddenFormLink' href='?following=1' data-userid=102>Following</a> • <a class='hiddenFormLink' href='?followers=1' data-userid=102>Followers</a></p>";
echo "<p><a class='hiddenFormLink' href='?following=1' data-userid=103>Following</a> • <a class='hiddenFormLink' href='?followers=1' data-userid=103>Followers</a></p>";
echo "<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script>";
echo "<script type='text/javascript'>
console.log('jq');
$('.hiddenFormLink').click(function(e){
console.log('data-userid=' + $(this).attr('data-userid') + ', value=' + $('.hiddenFormUserid').val());
e.preventDefault();
$('.hiddenFormUserid')
.val($(this).attr('data-userid'));
$('.hiddenForm')
.attr('action',$(this).attr('href'))
.submit();
});
</script>";
if (isset($_GET["following"]) || isset($_GET["followers"])) {
if (isset($_GET["following"])) {
echo "followed by ";
} else {
echo "followers of ";
}
if (isset($_POST["userid"])) {
echo $_POST["userid"]."<br>";
} else {
echo "no post<br>";
}
} else {
echo "no get<br>";
}
echo "</body></html>";
$_POST["userid"] = "";
?>
回答by Ignacio Vazquez-Abrams
Items in the query string are available via $_GET
, not$_POST
, since they are not actually POSTed. If you want to POST then you must either use a form with a method
of post
, or you must perform a XHR as POST.
在查询字符串的项目是通过提供$_GET
,不$_POST
,因为他们实际上并没有公布。如果要 POST,则必须使用带有method
of的表单post
,或者必须将 XHR 作为 POST 执行。