可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a form which I need to POST to multiple scripts. How can I do this the simplest way?
I know it could be done with Javascript, Curl or Snoopy class, but really which is the most simplest and best way to do this.
One of the scripts sends email and it's a php file, the other is hosted elsewhere.
Need to collect data on both scripts.
回答1:
The best way to go about it would be to first submit the form to your local script, then use CURL to POST the (filtered) data that was received to the remote script. Then just watch for the response.
Then simply send the email and process the response from the remote script in your local one.
回答2:
The easiest way to do this is to use jQuery to send an $.ajax (or $.post or $.get) to each script, retrieving the result from each of them and doing what you will with the results.
$(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);
});
});
});
update:
The serialize command is the data that is being sent. Take a look at the jQuery serialize function. It basically just takes the various inputs, selects, textareas, checkboxes, etc in your form, and puts them into a string like this:
myNameInput=john&active=on&whateverSelected=3
It's just a string of your form element names and their values. That is what is sent to the external script via the ajax command.
A side note, when doing serialize, make sure that all your form elements have a name attribute, not just an id. The serialize doesn't pay any attention to their id's. Only their name.
回答3:
This is what ended up working to Submit Form to Multiple Hosts using one action.
I posted usual mail script then included code to post to second host using Curl.
Below is the actual code that worked with some minor tweaks, like not having to use 'extract'.
Found at (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);
回答4:
I just had to do this with ASP. I wanted to post a form to an email processing script on one domain and record the action on a MySQL database on a different domain with one button click. This could also be useful in any number of other situations.
There is no way to have multiple ACTIONS on an HTML form definition. So, just send it on to one location, process the form, and then "repost" the form to another location. You can do this in a script "chain" of any length.
First add this ASP sub rountine to your script. If you are using PHP or some other language you can just translate this code to your own language and the concept will work.
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
Then, at the end of your process, just finish with a call to the sub like this:
RePost "http://www.SomeOtherDomain.com/SomeOtherScript.asp"
If needed, repeat the chaining processes on all your scripts, and then in the end you probably want to redirect to a page on your orginal domain (where the form came from) or do something to display a success message to your users.