views:

54

answers:

3

I have a html form with action="script1.php"

In script1 I need write all data to the database and redirect to script2.php, but I need all parameters posted to script1 to be sent to script2. mod_rewrite is on

How I can redirect using PHP with all data come through POST ?

if i do like that this disgusting practice but

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function Search(){
wpc_form.submit();
}
// -->
</script>
</HEAD>
<BODY onload='Search()'>
 <form name=wpc_form  method="post" action="/script2/">
 <?php
foreach($_REQUEST as $name => $value)
echo '<input type="hidden" name="'.$name.'" value="'.$value.'">'
 ?>

 </form>
+2  A: 

Impossible.
But you don't need it. Because you have all this data already. Just read it from the database in script2.php

Col. Shrapnel
script2 it service not mine in script1 i write data to database but also i need send there to the script2
Alehandro
@Alehandro What's stopping script2 from getting the data from the database?
NullUserException
script2(urlService) Service to run the logic necessary to transmit some data in POST(it not mine service i dont have access to them )
Alehandro
@Alehandro well, if it's not your own service, let user to communicate with this service directly, do not interfere.
Col. Shrapnel
if i do form with hiden fild and using js submit them ?i'ts disgusting practice?
Alehandro
A: 

A redirect doesn't allow you to do this unless you have custom client-side code running in the browser to extract state from the response message body in order to populate your form fields. This is advanced usage and probably not what you really want to do.

If you really do need to transmit state between your forms then you can use the session to do this. The form in the browser won't have access to the data, but your PHP script running on the server can store values between requests. Here's a link to a tutorial on sessions in PHP which might be of use to you. This approach is often used for maintaining application state between requests and redirects to third-party services such as OpenID providers etc.

Richard Cook
Thank you Richard Cook .I know what a session in this problem it is not suitable.
Alehandro
An alternative might then to be encode the parameters in the redirected URL. The form can then access the parameters via JavaScript and populate the form that way.
Richard Cook
A: 

You can use the cURL library (or similar) to send a separate POST request from your local script to the external service.

// assemble data from your post here:
$data = array('formfield' => 'data', 'otherfield' => 'otherdata');

// and then send it off somewhere else
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://somewhere.else');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
tadamson
it's not redirected((
Alehandro
i try example in manual curl it not redirect too ?
Alehandro
Right, it won't redirect, it's a separate request. You'd have to handle any redirection yourself in the rest of the script.
tadamson
after all code above i use header("Location:http://somewere.else"); just adding refreshed data ?
Alehandro
$data = $_POST; $ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://somewhere.else');curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);curl_exec($ch);header("Location:someURL");it send get request (with out parameters data)what wrong i do ?
Alehandro
It should be a POST, unless the external service is doing something. If the service is redirecting internally, making it a GET, try adding `curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true)`. And if it's expecting an HTTP referer header, you can set that with `curl_setopt($ch, CURLOPT_REFERER, /*value*/)`. And check `curl_error($ch)` to find anything else that went wrong.
tadamson