views:

168

answers:

4

Hi everyone,

I'm currently trying to get a script to submit a form to a page that is external to my site but will also e-mail the answers given by the customer to me. The mail() function has worked fine for the mail... but how do I then take these values and also submit them to the external page?

Thanks for your help!

A: 

For POST, you'll need to set the external page as the processing action:

<form action="http://external-page.com/processor.php" method="POST">
    <!-- Form fields go here --->
</form>

If it's GET, you can either change the form method to GET, or create a custom query string:

<a href="http://external-page.com/processor.php?field1=value1&amp;field2=value2"&gt;submit&lt;/a&gt;

Edit: I just realized you probably want to send these from within your PHP processing class. In that case, you could use set the location header with the custom query string:

header("Location: http://external-page.com/processor.php?field1=value1&amp;field2=value2");
Pat
No, that's not the problem. Currently, the form already does the above to send to the submit form which has the mail function inside it. So how would I submit it to the external page too?
David Archer
+2  A: 

If you get the form to submit to your script, can could first send the email and then use cURL to make a HTTP request to the external page, POSTing the values you want to send. This won't work though if the external site is relying on any cookies the user has, because the request is made from your web server.

e.g.

<?php
//data to post
$data = array( 'name' => 'tom', 'another_form_field'=>'a' ); 

//external site url (this should be the 'action' of the remote form you are submitting to)
$url = "http://example.com/some/url";  

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_POST, 1);  
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

//make curl return the content returned rather than printing it straight out
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  

$result = curl_exec($curl);

if ($result === false) {
    //curl error   
}

curl_close($curl);

//this is what the webserver sent back when you submitted the form
echo $result;
Tom Haigh
Hate to ask, but how would I go about this?
David Archer
Amazing, example worked like a dream, thank you!
David Archer
A: 

You could send a custom HTTP POST request from the script that you're using to send the email. Try fsockopen to establish the connection and then send your own HTTP request containing the data you just received from the form.

Edit:

A bit more specific. There's this example that shows you how to send a simple HTTP POST request. Just seed it with your $_POST array like this:

do_post_request(your_url, $_POST);

and that should do the trick. Afterwards, you could optionally evaluate the response to check whether everything went OK.

JorenB
That doesn't seem to have worked...
David Archer
A: 

You're going to have to dig through the source of the external form to determine the HTML name's of the relevant fields and whether the form is submitted using GET or POST.

If the form uses the GET method, you can easily generate a query-string that follows the same form as the actual form: http://example.com/form.php?name1=value1&amp;name2=value2 ...

If, on the other hand, the form uses the POST method, you'll have to generate a HTTP POST request using something like the cURL library (http://us2.php.net/curl).

Mike Koval