PHP Form POST to external URL with Redirect to another URL

Posted by Marlon on Stack Overflow See other posts from Stack Overflow or by Marlon
Published on 2010-05-03T01:29:23Z Indexed on 2010/05/03 1:38 UTC
Read the original article Hit count: 418

Filed under:
|
|
|
|

So, what I am trying to accomplish is have a self-posting PHP form, POST to an external page (using CURL) which in turn redirects to another page. Currently, what is happening is that once I click "Submit" on the form (in contact.php) it will POST to itself (as it is a self-posting form). The script then prepares the POST using CURL and performs the post. The external page does its processing and then, the external page is supposed to redirect back to another page, in a referring domain. However, what happens instead, is that it seems like the contact.php page loads the HTML from the page the external page redirected to, and then, the contact.php's HTML loads after that, ON THE SAME PAGE.

The effect, is what looks like two separate pages rendered as one page. Naturally, I just want to perform the POST and have the browser render the page it is supposed to redirect to as specified by the external page.

Here is the code I have so far:

<?php     
if(isset($_POST['submit']))
 {
  doCURLPost();
 }

 function doCURLPost()
 { 
  $emailid = "2, 4";

  $hotel = $_POST['hotel'];


  //you will need to setup an array of fields to post with
  //then create the post string

  $data = array ( "recipient" => $emailid,
      "subject" => "Hotel Contact Form",
      "redirect" => "http://www.localhost.com/thanx.htm",
      "Will be staying in hotel: " => $_POST['hotel'],
      "Name" => $_POST['Name'],
      "Phone" => $_POST['Phone'],
      "Comments" => $_POST['Comments']);

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://www.externallink.com/external.aspx");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_POST, true);  
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Referer: http://www.localhost.com/contact.php"));
  $output = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);
 }
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about html