简体   繁体   中英

Submit a form and email it with PHP

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!

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.

eg

<?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;

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.

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&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 ).

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&field2=value2">submit</a>

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&field2=value2");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM