PHP POST Requests in PHP

How are you going to redirect the browser and send the HTTP POST request to PHP? A header("Location: file.php?foo=bar") of HTTP POST requests, if you do.

+4
source share
7 answers

You cannot - HTTP does not allow this - if you want to pass arguments through a redirect, they must be embedded in the URL as GET vars.

FROM.

+5
source

This does not redirect the browser, but can do a POST request.

Curl manual

Curl POST Example

PHP POST Without Curl

To redirect the browser, I would suggest using Javascript.

An example form that does POST and redirects

  <FORM action="http://somesite.com/prog/adduser" method="post"> <P> <LABEL for="firstname">First name: </LABEL> <INPUT type="text" id="firstname"><BR> <LABEL for="lastname">Last name: </LABEL> <INPUT type="text" id="lastname"><BR> <LABEL for="email">email: </LABEL> <INPUT type="text" id="email"><BR> <INPUT type="radio" name="sex" value="Male"> Male<BR> <INPUT type="radio" name="sex" value="Female"> Female<BR> <INPUT type="submit" value="Send"> <INPUT type="reset"> </P> </FORM> 
+3
source

I'm not sure why you need this, however it is not possible in any server language.

You can use javascript library like jQuery to request a page using send request

+2
source

I do not believe that you can get a browser for POST data by redirecting it in the middle of the request. You are limited to GET. If you want the browser to be POST, you need to build a <form> and submit it. (Or use an AJAX request.)

+1
source

PHP has nothing of the kind. To execute your example, you can simply say $_GET['foo'] = 'bar'; include("file.php") $_GET['foo'] = 'bar'; include("file.php") , however the URL assigned to the browser will not be changed.

Similar question: Code Translation: ASP.NET Server.Transfer in PHP

+1
source

This question has been asked here. How do you send POST to a page using the PHP header () function? .

Someone commented that if you already have data, why do you need to post it anywhere, why can't you just work on the data on this page?

+1
source

If you need to transfer data when redirecting without displaying the data in the URL, you can use $ _SESSION, first store the data in the session, and then redirect the page, after the redirection get the data from the session and destroy the session data.

OK, if you need to transfer data to another site without specifying in the URL, you will have to use Javascript instead ... for example, transfer data to payPal. just create a form and write javascript code to submit the form automatically when the page loads. The following is an example code:

 <form name="myform" action="handle-data.php"> Search: <input type='text' name='query' /> <a href="javascript: submitform()">Search</a> </form> <script type="text/javascript"> function submitform() { document.myform.submit(); } </script> 
+1
source

All Articles