It seems to me that you are trying to redirect from one page to another by sending POST data. I am afraid this is not possible with PHP.
The simplest workaround I can come up with is to use cURL to (1) send your $_POST data to another server, (2) get the result from that server, and then (3) make a simple redirect to another page. Although this requires only 3 HTTP requests (a bit for such a simple problem), it more or less achieves what you are trying to achieve.
Let me break this approach down to the three steps mentioned in your question:
For step 1, any form of HTML will do if it has a method attribute with a value of post. If you want to submit the form without using JavaScript, just make sure that it has a submit button. This will send your data to the page defined in the action attribute of your form when you click the submit button:
HTML example:
<form action="action_page.php" method="post"> First name:<br /> <input type="text" name="firstname" value="Mickey"><br /> Last name:<br /> <input type="text" name="lastname" value="Mouse" /><br /><br /> <input type="submit" value="Submit" /> </form>
For step 2, you need to create a page with a name identical to the value of the action attribute of your form. In our example, this will be action_page.php . Here you check if $_POST any values for the form fields that you submitted. In the case or our example, you can do something like this:
if (isset($_POST['firstname']) && isset($_POST['lastname'])) {
For step 3, you need to make a second POST request using the $_POST data you just received from your HTML form. The easiest way to do this is to use cURL. Then you redirect the user to the second page, depending on the result of your second POST request.
To improve readability of the sample code, I put all the code associated with the cURL request into a function:
function forwardRequest($url, $data) { $result = [ 'status' => NULL, 'last_url' => NULL, 'response' => NULL ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $result['response'] = curl_exec($ch); $result['status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); curl_close ($ch); return $result; } if (isset($_POST['firstname']) && isset($_POST['lastname'])) {
Note 1:
I am afraid that there is no general way to determine if everything is going well in an external system when data is sent to that system.
In my sample code, I use the status code of the HTTP response received by your cURL request. This solution only works when the server returns a status code that is not 200, if something is wrong on this server. You will need to run a few tests to make sure this approach is enough for your use.
If you cannot use the status code to determine if everything went well or not, consider checking the last effective URL or searching for keys in the response header and response text.
Note 2:
Instead of forwarding your user after processing the cURL request, you may just need to show the response text from the server to your user. If this result contains a lot of unwanted information that you do not need, consider parsing the result using DOMDocument::loadHTML or a third-party library (for example, Masterminds / html5 -php or PHPPowertools / DOM-Query ).
You can also simply write your own response text.