Question:
PHP: How to send POST request to another page?
Twist of Fate
2007-04-14 08:02:09 UTC
To send GET request to another page is easy...

header("location: page2.php?string=hello");


But how do I send/redirect POST request to page2.php??
Three answers:
Yevgeny
2007-04-14 08:29:47 UTC
IMPORTANT: you need to have the cURL library installed for this to work!



page1.php

- - - - - - - - - - - -


$c = curl_init();

curl_setopt($c, CURLOPT_URL, 'http://fullurl/page2.php');

curl_setopt($c, CURLOPT_POST, true);

curl_setopt($c, CURLOPT_POSTFIELDS, 'firstName=John&lastName=Doe ');

curl_exec ($c);

curl_close ($c);

?>



page2.php

- - - - - - - - - - - -


$fn = $_POST['firstName'];

$ln = $_POST['lastName'];

// test to see that it really works

file_put_contents('post.txt', $fn . $ln);

?>
anonymous
2007-04-14 08:51:15 UTC
Your POST variables should be preserved if you use header() to redirect to a different page; Location sends the browser a 302 moved permanently response code.



If that doesn't work, you could try sending a 307 moved temporarily http response:



header("HTTP/1.0 307 Temporary redirect");

header("Location:page2.php");
leila
2016-05-20 02:43:05 UTC
Use an When you submit the original form the form's action should be set to email.php and in that page you must have the hidden input so it records the post data so it can be re posted when the email form is submitted.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...