ServerSide Form Validation, then post ENTIRE PAGE to URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
covareo
Forum Newbie
Posts: 1
Joined: Thu Apr 19, 2007 2:34 pm

ServerSide Form Validation, then post ENTIRE PAGE to URL

Post by covareo »

Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Server Side Form Validation, then post ENTIRE PAGE (not just headers) to external URL:

Alright. I hope you guys can help me! I've asked this question on so many boards I am about to give up! I have an app that the client is requiring server side form validation on, then the validated form data needs to be posted to an external URL to which we have no access (salesforce.com). I can get the form data to post just fine, but the page itself does not post because the form action field is (and must remain) the form/processing page, not the external salesforce.com URL. Ideally the entire page must post (along with the form POST data) as if the form's action was originally the external URL AFTER the data has been validated.

Heres my code so far - works, but the page just posts to itself after handing off POST headers to $URL.

Code: Select all

<?

function post_it($url)
{
  $saveurl = $url;  
 
  $url  = preg_replace("@^https://@i", "", $url);  
  $host = substr($url, 0, strpos($url, "/"));
  $uri  = strstr($url, "/");
            
  $reqbody = "";
 
  foreach($_POST as $key=>$val)  {  

    if (is_array($val)) {      
      if (!empty($reqbody)) $reqbody .= "&";
      $reqbody .= $key . "=" . $val;
    }
    else {if (!empty($reqbody)) $reqbody .= "&"; $reqbody .= $key . "=" . urlencode($val);}      
  }

  $reqlength = strlen($reqbody);

  $reqheader = "POST $uri HTTP/1.0\r\n".
                      "Host: $host\r\n" . "User-Agent: PostIt\r\n".
                      "Content-Type: application/x-www-form-urlencoded\r\n".
                      "Content-Length: $reqlength\r\n\r\n".
                      "$reqbody\r\n";
 

  header("Location: $saveurl");

  $socket = fsockopen("ssl://" . $host, 443, $errno, $errstr);

  if (!$socket) {$result["errno"] = $errno; $result["errstr"] = $errstr; return $result;}
  fputs($socket, $reqheader);
  while (!feof($socket)) {$result[] = fgets($socket, 4096);}
  fclose($socket);
      
  return $result;
}

    $isvalidated       = 1;
      $emailerror          = "";
    $emailclass       = "basictext1";
      $emailpattern       = '/^[[]_\.\-]+@([[]_\.\-]+\.)+[[]]{2,4}$/';
    $fullnamerror       = "";
      $fullnameclass      = "basictext1";
      $fullnamepattern= '/[a-zA-Z ]{1,}/';
      $phoneerror            = "";
      $phoneclass            = "basictext1";
      $phonepattern      = '/[\(.]?[2-9]\d\d[\).]?[ -]?[2-9]\d\d[-.]\d{4}/';
      $phone                  = "";

    if ($_POST['process'] == 1) {
       
            if (preg_match($emailpattern, $_POST['email']) < 1) {
            $emailerror          = "Please enter a valid email address.";
                  $emailclass       = "errortext";
                  $isvalidated      = 0;
        }else{
                  $email = $_POST['email'];
            }
            
            if (preg_match($fullnamepattern, $_POST['name']) < 1) {
            $fullnameerror          = "Please enter your name.";
                  $fullnameclass       = "errortext";
                  $isvalidated      = 0;
        }else{
                  $name = $_POST['name'];
            }
            
            if (preg_match($phonepattern, $_POST['phone']) < 1) {
            $phoneerror          = "Please enter your phone number.";
                  $phoneclass       = "errortext";
                  $isvalidated      = 0;
        }else{
                  $phone = $_POST['phone'];
            }
            
            if ($isvalidated == 1) {
            
            $url = "https://www.externalsite.com/fakeformactiontarget.php";
            
            post_it($url);

            }
            
    }
?>

<html>
<style>
.basictext1 {
    font: Arial, Helvetica, sans-serif 12px;
}
.errortext {
    font: Arial, Helvetica, sans-serif 12px bold;
    color:#CC0000;
}
</style>
<body>
<form action="thispage.php" method="post">

      <div>
      <label for="name" class="<? print $fullnameclass; ?>">Name:</label>
      <input name="name" type="text" class="textbox" id="name" value="<? print $name; ?>" />
      <? if ($fullnameerror != "") {
            print '<br /><span class="errortext">'.
                  $fullnameerror."</span>\n";
      }
      ?>
      </div>                  
      <div>
      <label for="email" class="<? print $emailclass; ?>">E-mail Address:</label>
      <input name="email" type="text" id="email" value="<? print $email; ?>" />
      <? if ($emailerror != "") {
            print '<br /><span class="errortext">'.
                  $emailerror."</span>\n";
      }
      ?>
      </div>
      <div>
      <label for="phone" class="<? print $phoneclass; ?>">Phone Number:</label>
      <input name="phone" type="text" id="phone" value="<? print $phone; ?>" />
                                          <? if ($phoneerror != "") {
                                                print '<br /><span class="errortext">'.
                                                      $phoneerror."</span>\n";
                                          }
                                          ?>
      </div>
      <input type="hidden" name="process" value="1">
      <input type="hidden" name="oid" value="xxx">
      <input type="hidden" name="retURL" value="http://www.somesite.com/wheresalesforcesendsus.php">
      <input name="Submit" type="submit" value="Submit" class="buttons" />
</form>
</body>
</html>
Jcart | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Look at curl in the php docs. You should be able to build a request and post the data to salesforce.com.
Post Reply