【发布时间】:2009-08-21 14:53:09
【问题描述】:
我目前正在尝试获取一个脚本,以将表单提交到我网站外部的页面,但也会通过电子邮件将客户给出的答案发送给我。 mail() 函数在邮件中运行良好......但是我如何获取这些值并将它们提交到外部页面?
感谢您的帮助!
【问题讨论】:
标签: php javascript forms
我目前正在尝试获取一个脚本,以将表单提交到我网站外部的页面,但也会通过电子邮件将客户给出的答案发送给我。 mail() 函数在邮件中运行良好......但是我如何获取这些值并将它们提交到外部页面?
感谢您的帮助!
【问题讨论】:
标签: php javascript forms
如果您将表单提交到您的脚本,可以先发送电子邮件,然后使用cURL 向外部页面发出 HTTP 请求,发布您要发送的值。如果外部网站依赖于用户拥有的任何 cookie,这将不起作用,因为请求是从您的网络服务器发出的。
例如
<?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;
【讨论】:
您将不得不挖掘外部表单的来源以确定相关字段的 HTML name 以及该表单是使用 GET 还是 POST 提交的。
如果表单使用 GET 方法,您可以轻松生成与实际表单相同的查询字符串:http://example.com/form.php?name1=value1&name2=value2 ...
另一方面,如果表单使用 POST 方法,则必须使用 cURL 库 (http://us2.php.net/curl) 之类的东西生成 HTTP POST 请求。
【讨论】:
对于 POST,您需要将外部页面设置为处理操作:
<form action="http://external-page.com/processor.php" method="POST">
<!-- Form fields go here --->
</form>
如果是 GET,您可以将表单方法更改为 GET,或者创建自定义查询字符串:
<a href="http://external-page.com/processor.php?field1=value1&field2=value2">submit</a>
编辑:我刚刚意识到您可能希望从您的 PHP 处理类中发送这些。在这种情况下,您可以使用自定义查询字符串设置位置标头:
header("Location: http://external-page.com/processor.php?field1=value1&field2=value2");
【讨论】: