如何通过链接传递POST变量到自己的页面? [英] how to pass POST variable by links to own pages?

查看:107
本文介绍了如何通过链接传递POST变量到自己的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过链接到自己的页面获得变量$ _POST。示例:

Hi i wannna get variable $_POST by link to self pages. Example :

<?PHP
$var = 'PIG';
echo "<a href='test.php?var=$var'>link</a>";

if (isset($_POST['var']))
{
echo $_POST['var']);
}


?>

它链接到自己的页面。 (test.php)
它不起作用,谁可以帮助我。谢谢

it links to own pages. (test.php) It not works, who can help me please. Thanks

推荐答案

链接不能 POST 数据,只有 GET


与GET请求方法相比,只有URL和标题是
发送到服务器后,POST请求还包括一个消息体。这个
允许将任意类型的任意长度数据发送到服务器。

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.

基本上, POST 需要两个请求,1)服务器收到正常请求,额外的标头值表示需要发送更多数据。此时,服务器发送确认,2)客户端发送 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.

然而,有解决方案,我已经看到一些技术,其中包括输出表格带有自动提交,类似于

However, there are solutions to this and I have seen some technique, among others, outputting a form with an autosubmit, something like

<form name="frm" method="post" action="http://your.domain.com/path/to/page.php?param1=1&param2=2">
<input type="hidden" name="foo" value="bar" />
</form>
<script type="text/javascript">
    document.forms["frm"].submit();
</script>

这会导致调用 page.php 使用这些参数

which would result into calling page.php with these arguments

$_GET = array('param1' => '1', 'param2' => '2');
$_POST = array('foo' => 'bar');

请注意,这是一个简单的重定向方法,但您可以创建 < a> 元素实际触发一些隐藏的形式,而不是使用标准链接。 (未经测试的代码)

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 href="http://your.domain.com/path/to/page.php?param1=1&param2=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.

这篇关于如何通过链接传递POST变量到自己的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆