PHP将POST变量转发到其他页面

时间:2013-04-08 18:46:25

标签: php javascript html

我有html表单,我必须在该表单的同一页面上提交该表单,添加POST变量,然后所有变量都传递到下一页。我试过这个:

     <form method="POST" action="">
        <input type="TEXT" name="names" />
        <input type="TEXT" name="email" />
        <input type="submit" name="send" />
     </form>

然后这个PHP代码:

if($_POST['send']){

    $_POST['digest'] = "someText here";
    header("HTTP/1.0 307 Temporary redirect");
    header("Location:https://nextpage.com/form");

}

但是当我被重定向到另一个页面时,所有POST数据都被发送,除了“$ _POST ['digest']”..我该怎么办?

感谢并抱歉英语不好。

3 个答案:

答案 0 :(得分:2)

您需要传递要重定向到的网址的查询字符串中的变量。

请参阅http://www.php.net/manual/en/function.http-build-query.php

答案 1 :(得分:2)

如果您使用的是php的头功能,则无法通过POST重新传输变量。

这里有两种选择:

  1. 在$ _GET中转换$ _POST(例如http_build_query)
  2. 如果您必须将此数据作为POST传输,则可以创建包含输入类型=“隐藏”的表单的空白页面,然后使用javascript提交。有点难看,但应该工作。

答案 2 :(得分:2)

你需要使用cURL。

    $fields_string = "name=".$_POST['name']."&email=.$_POST['email'];
    $url = "the website you want to post to";
    $cx = curl_init();
        curl_setopt($cx, CURLOPT_URL,$url);
        curl_setopt($cx, CURLOPT_POST, true);
        curl_setopt($cx, CURLOPT_POSTFIELDS,$fields_string);
        curl_setopt($cx, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($cx, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($cx, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cx, CURLOPT_FOLLOWLOCATION, FALSE);
    $init_response = curl_exec($cx);
    curl_close($cx);

http://php.net/manual/en/book.curl.php

相关问题