How do you go about redirecting a browser and sending a HTTP POST request in PHP? A header("Location: file.php?foo=bar")
of HTTP POST requests, if you will.
views:
333answers:
7I don't believe you can get a browser to POST data by redirecting it in the middle of a request. You're limited to GET. If you want a browser to POST something you need to construct a <form>
and submit it. (Or use an AJAX request.)
PHP doesn't have anything like this. To fulfill your example, you can just simply say $_GET['foo'] = 'bar'; include("file.php")
, however the URL given to the browser will not be changed.
Similar question: http://stackoverflow.com/questions/471014/code-translation-asp-net-server-transfer-in-php
This does not redirect the browser but it can perform a POST request.
To redirect the browser i'd suggest using Javascript.
An example form that does POST and redirect
<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>
You can't - HTTP does not allow this - if you want to pass arguments via a redirect they have to be embedded into the URL as GET vars.
C.
This question was asked here http://stackoverflow.com/questions/653090/how-do-you-post-to-a-page-using-the-php-header-function.
Someone commented that if you already had the data why do you need to post it anywhere, why can't you just act on the data in that page?
If you need to transfer data when you redirect without showing data in the URL, you can use $_SESSION, first store data into session then redirect the page, after redirection get data from session and destroy the session data..
OK, if you need to transfer data to other site without showing in the URL, u have to use Javascript instead... like transfer data to payPal. just make a form and write a javascript code to submit the form automatically on page load. below is the sample 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>