1

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.

Haris Maroš
  • 19
  • 1
  • 2
  • You'd have to add `digest` to the URL (which of course would turn it into a GET variable, with length limits etc.) I don't think there is a way to do exactly what you want. (Actually I'm surprised that browsers re-post POST data to the new URL. Is this documented/expected?) – Pekka Apr 08 '13 at 18:49
  • Look at http://stackoverflow.com/questions/653090/how-do-you-post-to-a-page-using-the-php-header-function – Heberfa Apr 08 '13 at 18:51
  • 1
    @Pekka웃: it may vary from browser to browser. See this post: http://stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get – Alekc Apr 08 '13 at 18:52
  • @Alekc thanks! That's useful info. – Pekka Apr 08 '13 at 18:53
  • Before I answer, do you wish to send data to a file on the same server? then manipulate this on one page (located on the same server)? – Daryl Gill Apr 08 '13 at 19:09

3 Answers3

2

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

abdel
  • 73
  • 1
  • 5
  • 1
    @Haris why is it not what you need? It helps you build a URL for the next page – Pekka Apr 08 '13 at 18:54
  • 1
    @Haris it's impossible to add POST data to the browser's request from the PHP end. You'll have to go with Alekc's workaround then. – Pekka Apr 08 '13 at 18:58
2

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.
Alekc
  • 4,682
  • 6
  • 32
  • 35
2

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

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • This would fundamentally change what the OP is doing. There would no longer be any of the browser's context (an open session, cookies, client IP, user agent...) – Pekka Apr 08 '13 at 18:51
  • @Pekka웃 why wouldn't there be? the contents of the next page are stored in a variable you can display it how you want. what's stopping you from putting `session_start();` at the top of that page? I honestly don't understand your objection. – I wrestled a bear once. Apr 08 '13 at 19:00
  • with curl, *your server* is making the request, not the client's browser. Curl acts as a separate client, it has nothing to do with the user's browser. The user's session ID is unavailable to curl, no matter whether there's a `session_start()` in place or not. – Pekka Apr 08 '13 at 19:01
  • @Pekka웃 how is that relevant to the question though? he never said he needed the session.. he didn't even say if he was posting to another server. for all you know all the pages may be on the same server anyway.. – I wrestled a bear once. Apr 08 '13 at 19:03
  • @Pekka웃 and what makes you think the receiving page needs his useragent/ip anyway? – I wrestled a bear once. Apr 08 '13 at 19:05
  • +1 this is informative but is there any built-in PHP function to do a server-side redirect (just a simple hand-over) without using curl? I don't want to redirect via header() though. – Ravi K Thapliyal May 21 '13 at 14:04
  • 1
    I don't know what I was thinking that day. @Pekka웃 is totally right, thought OP could have made this work. – I wrestled a bear once. Aug 31 '15 at 13:08