Geoff Ellerby
Geoff Ellerby

Reputation: 170

Need form to post to two off-server URLs then redirect with PHP

I have a form that needs to be submitted to two separate URLs: one being a Google Spreadsheet to store the data and the other being an off-site ASP form handler script. When I set the form action to either URL it works perfectly. However, I need it to submit to both URLs then redirect to a simple success.html page. If they weren't external scripts I could do so easily with AJAX (see article) but that isn't an option because of cross domain issues. Here is my code:

<form name="apply-now-form" action="somescript.php" method="POST" id="apply-now-form"   class="apply-now-form">
   <label class="form-label" >Full Name</label>
     <input name="FirstName" id="FirstName" type="text">
     <input name="LastName" id="LastName" type="text" >
   <label>Phone Number</label>
     <input name="ResidencePhone" id="ResidencePhone" type="text" > 
   <label>Email Address</label>
     <input name="EmailAddress" id="EmailAddress" type="email" >
</form>

The ASP script handles the success redirect so I reckon it should be submitted to that one second. Is there a way, using cURL or otherwise, to send unaltered form data to multiple URLs using PHP? Or Javascript if that's an option? Thanks.

Upvotes: 1

Views: 239

Answers (2)

Iker V&#225;zquez
Iker V&#225;zquez

Reputation: 567

You could perform this with CURL.

1- Create the data array and serialize it:

$fields = array( $k => $v,...);

 foreach ($fields as $key => $value) {
        $fields_string .= $key . '=' . $value . '&';
    }
 rtrim($fields_string, '&');

2- Send the data to Google:

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://googlesite');
  curl_setopt($ch, CURLOPT_POST, count($fields));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);

3- Send the data to the ASP:

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'http://aspweb');
  curl_setopt($ch, CURLOPT_POST, count($fields));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  curl_close($ch);

4- Redirect

 header('Location: http://successpage')

I think that's all.

Upvotes: 1

jaco
jaco

Reputation: 3516

You named it... you have to use curl! A nice place to start could be PHP cURL manual: http://php.net/manual/en/book.curl.php

Upvotes: 0

Related Questions