如何 ?将帖子发布到多个位置

时间:2009-10-18 16:24:08

标签: php html-form

我有一个表单,我需要POST到多个脚本。我怎样才能以最简单的方式做到这一点?

我知道可以使用Javascript,Curl或Snoopy类完成,但实际上这是最简单和最好的方法。

其中一个脚本发送电子邮件,它是一个php文件,另一个托管在其他地方。

需要收集两个脚本的数据。

4 个答案:

答案 0 :(得分:5)

最好的方法是先将表单提交到本地脚本,然后使用CURL将收到的(已过滤的)数据发送到远程脚本。然后只是注意回应。

然后只需发送电子邮件并处理来自本地远程脚本的响应。

答案 1 :(得分:3)

最简单的方法是使用jQuery向每个脚本发送$ .ajax(或$ .post或$ .get),从每个脚本中检索结果,然后按照结果执行操作。< / p>

$(document).ready( function(){
    $('#mySubmitButton').click(function(){
        //Send data to the email script
        $.post( 'send-email.php', $('form').serialize(), function(data, textStatus) {
            //data is the result from the script
            alert(data);
        });

        //Send data to the other script
        $.post( 'my-other-script.php', $('form').serialize(), function(data, textStatus) {
            //data is the result from the script
            alert(data);
        });
    });
});

<强>更新 serialize命令是正在发送的数据。看一下jQuery序列化函数。它基本上只需要在表单中输入各种输入,选择,textareas,复选框等,并将它们放入如下字符串中:

myNameInput =约翰&安培;活性= ON&安培; whateverSelected = 3

它只是表单元素名称及其值的字符串。这是通过ajax命令发送到外部脚本的内容。

附注,在进行序列化时,请确保所有表单元素都具有 name 属性,而不仅仅是 id 。序列化不会关注他们的 id的。只有他们的名称

答案 2 :(得分:1)

这就是最终使用一个操作向多个主机提交表单的工作。

我发布了通常的邮件脚本,然后包含使用Curl发布到第二个主机的代码。

下面是一些实际的代码,它们可以进行一些小的调整,比如不必使用'extract'。

找到(http://php.dzone.com/news/execute-http-post-using-php-cu):

//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
                        'lname'=>urlencode($last_name),
                        'fname'=>urlencode($first_name),
                        'title'=>urlencode($title),
                        'company'=>urlencode($institution),
                        'age'=>urlencode($age),
                        'email'=>urlencode($email),
                        'phone'=>urlencode($phone)
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

答案 3 :(得分:1)

我只需要用ASP做到这一点。我想将表单发布到一个域上的电子邮件处理脚本,并通过单击按钮将操作记录在不同域上的MySQL数据库中。这在任何其他情况下也可能有用。

HTML表单定义无法使用多个ACTIONS。因此,只需将其发送到一个位置,处理表单,然后将表单“重新发布”到另一个位置。您可以在任何长度的脚本“链”中执行此操作。

首先将此ASP子程序添加到您的脚本中。如果您使用的是PHP或其他语言,您只需将此代码翻译成您自己的语言,这个概念就可以使用。

sub RePost( Destination )
    RePostString = "<HTML><BODY>" & vbCRLF
    if( (Trim(Destination) <> "") and (Request.ServerVariables("REQUEST_METHOD") = "POST") ) then
        RePostString = RePostString & "<FORM METHOD=POST NAME=""RePostForm"" ACTION=""" & Destination & """>" & vbCRLF
        for each Item in request.form
            if( not len(item) <= 0 ) Then
                RePostString = RePostString & "<INPUT TYPE=HIDDEN NAME=""" & item & """ VALUE=""" & Request.form( item ) & """>" & vbCRLF
            end if
        next
        RePostString = RePostString & "</FORM>" & vbCRLF & _
                       "<script language=""JavaScript"" type=""text/javascript""> window.onLoad = document.RePostForm.submit(); </script>"
    else
        RePostString = "<CENTER><H1><BR><BR>Sorry! Internal Scripting Error Encountered!</H1></CENTER>" & vbCRLF
    end if
    RePostString = RePostString & "</BODY></HTML>"
    Response.Write( RePostString )
end sub

然后,在你的过程结束时,只需完成对sub的调用:

 RePost "http://www.SomeOtherDomain.com/SomeOtherScript.asp"

如果需要,请在所有脚本上重复链接过程,最后您可能希望重定向到原始域(表单来自哪里)上的页面,或者执行某些操作以向用户显示成功消息