Haris Maroš
Haris Maroš

Reputation: 19

PHP forward POST variables to other page

I have html form and I have to submit that form on same page where's that form, add POST variable and then all variables pass to next page. I tried this:

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

and then this PHP Code:

if($_POST['send']){

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

}

But when i get redirected to another page, all POST data are sent except "$_POST['digest']".. What should I do ?

Thanks and sorry for bad english.

Upvotes: 1

Views: 6790

Answers (3)

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

you need to use cURL for this.

    $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

Upvotes: 2

Alekc
Alekc

Reputation: 4770

You can't retransmit your variables via POST if you are using header function of php.

You have 2 alternatives here:

  1. convert $_POST in $_GET (for example http_build_query)
  2. if it's essential for you to have this data to be transmitted as POST, you can create a blank page containing form with input type="hidden" and then just submit it with javascript. A bit ugly but should work.

Upvotes: 2

abdel
abdel

Reputation: 73

You need to pass the variables in the query string of the url you are redirecting to.

See http://www.php.net/manual/en/function.http-build-query.php

Upvotes: 2

Related Questions