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?
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.
You simply can't redirect a POST request via standard http methods.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With