Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Redirect and send data via POST

I have an online gateway which requires an HTML form to be submitted with hidden fields. I need to do this via a PHP script without any HTML forms (I have the data for the hidden fields in a DB)

To do this sending data via GET:

header('Location: http://www.provider.com/process.jsp?id=12345&name=John'); 

And to do this sending data via POST?

like image 958
Robin Rodricks Avatar asked Jun 15 '10 12:06

Robin Rodricks


People also ask

How redirect data from one page to another in PHP?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

Can we redirect to post method?

You simply can't redirect a POST request via standard http methods.

What is redirect () in PHP?

PHP. Redirection allows you to redirect the client browser to a different URL. You can use it when you're switching domains, changing how your site is structured, or switching to HTTPS. In this article, I'll show you how to redirect to another page with PHP.


2 Answers

You can't do this using PHP.

As others have said, you could use cURL - but then the PHP code becomes the client rather than the browser.

If you must use POST, then the only way to do it would be to generate the populated form using PHP and use the window.onload hook to call javascript to submit the form.

like image 101
symcbean Avatar answered Oct 02 '22 17:10

symcbean


here is the workaround sample.

function redirect_post($url, array $data) {     ?>     <html xmlns="http://www.w3.org/1999/xhtml">     <head>         <script type="text/javascript">             function closethisasap() {                 document.forms["redirectpost"].submit();             }         </script>     </head>     <body onload="closethisasap();">     <form name="redirectpost" method="post" action="<? echo $url; ?>">         <?php         if ( !is_null($data) ) {             foreach ($data as $k => $v) {                 echo '<input type="hidden" name="' . $k . '" value="' . $v . '"> ';             }         }         ?>     </form>     </body>     </html>     <?php     exit; } 
like image 37
rezashamdani Avatar answered Oct 02 '22 15:10

rezashamdani