How To ? Form Post to Multiple Locations

Posted on

How To ? Form Post to Multiple Locations – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and forms . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about How To ? Form Post to Multiple Locations.

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.

Solution :

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.

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(){
        //Send data to the email script
        $.post( 'send-email.php', $('form').serialize(), function(data, textStatus) {
            //data is the result from the script

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

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:


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.

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 (

//extract data from the post

//set POST variables
$url = '';
$fields = array(

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

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data

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

//close connection

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
        RePostString = RePostString & "</FORM>" & vbCRLF & _
                       "<script language=""JavaScript"" type=""text/javascript""> window.onLoad = document.RePostForm.submit(); </script>"
        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 ""

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.

Leave a Reply

Your email address will not be published. Required fields are marked *