Jump to content

Validate Form then Submit to External Site


ultrasound0000

Recommended Posts

I have a form that I'm initially validating with PHP on the same page.  When all is validated, I need to submit the form data to an external page (SalesForce).

 

I've figured out how to do the validating in PHP but now I'm kind of stuck on the external form submission part.

 

Here is a code snippet:

 

<?php

        $error    	= "";
        $msg		= "";									
        $pattern 	= '/.*@.*\..*/';
        $first_name = $_POST['first_name'];
        $last_name  = $_POST['last_name'];
        $company   	= $_POST['company'];
        $phone   	= $_POST['phone'];
        $email   	= $_POST['email'];
        
        
        if ($_POST['process'] == 1) {
            
            if ( $_POST['first_name'] == '' ) {
                $error = 'Please enter your name';
            }
            elseif ( $_POST['last_name'] == '' ) {
                $error = 'Please enter your last name';
            }
            elseif ( $_POST['company'] == '' ) {
                $error = 'Please enter your company';
            }
            elseif ( $_POST['phone'] == '' ) {
                $error = 'Please enter your phone number';
            }										
            elseif (preg_match($pattern, $_POST['email']) == 0) {
                $error = "Please enter a valid email address.";											
                $emailclass = "errortext";
            }
[color=red][b]            else {
                    $msg = "OK";
                    
                }
[/b][/color]            }
        
        }
      ?>
      
      <?php 
            if ($msg == "OK") {
                echo "<p><strong>Thank you for your submission.</strong></p>";
                
            }
            else {
      ?>
      <form id="myForm" action="self.php" method="post" >
         
        <?php 
            if ( $error ) ?>
           <p class="errortext"><?php print $error; ?></p>
        
      
        <label>First Name*</label>
        <span class="txt"><input id="first_name" name="first_name" type="text" tabindex="1" value="<?php print $first_name;?>"  class="<? print $nameclass; ?>" /></span>
        
        <label for="last_name">Last Name*</label>
        <span class="txt"><input id="last_name" name="last_name" type="text" tabindex="2" value="<?php print $last_name;?>" /></span>
        
        <label for="company">Company*</label>
        <span class="txt"><input id="company" name="company" type="text" tabindex="3" value="<?php print $company;?>" /></span>

        <label for="phone">Phone Number*</label>
        <span class="txt"><input id="phone" name="phone" type="text" tabindex="4" value="<?php print $phone;?>" /></span>

        <label for="email">Email*</label>
        <span class="txt"><input id="email" name="email" type="text" tabindex="5" value="<?php print $email;?>" /></span>

        <span>
             <input type="hidden" name="process" value="1">
            <input class="submitBtn" type="submit" id="submitt" name="submit" tabindex="6" value="" /><br style="clear:both;" />										
            <em>*Required Fields</em>
        </span>
       </form>
       <?php } ?>                                  

 

I wonder if somewhere here I can redirect to the page once all is validated:

 

[color=red][b]            
else {
        $msg = "OK";
        PROCESS THE EXTERNAL FORM SUBMISSION
}
[/b][/color]

 

ANY TIPS/IDEAS GREATLY APPRECIATED!

 

 

I don't know what exactly you are trying to do, but it's probably a better idea to store the data in a database and only pass a id to the other scripts.

 

If you really want to post it to another web page you can use cUrl like that :

<?php
   $timeout = 10;
   $post = "field1=test&field2=something";
   $ch = curl_init("http://www.somesite.com/somepage.php");
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   $output = curl_exec($ch);
   curl_close($ch);
?>

Thanks to both of you for replying.

 

I don't have the Curl library installed and also I don't have a DB back-end.

 

Up until now I have been validating my form with JavaScript and then submitting the form to the external website that processes all the hidden variables that I pass and stores them into a database.

 

With the JavaScript validation, I the post action would = the external website.  On form submit, I'd run the JS code to validate the form input.  If all input was ok then I'd submit the form such as: document.forms["myForm"].submit();

 

Now, I'm trying to accomplish the same thing with PHP but for the time being cannot use Curl.  Any other options I'm left with or workarounds?

 

Without a database or cUrl, you have less good choice. But there lot of options you can use to transfer data.

 

You can use fsockopen and post the data with that. That probably what cUrl use to make the connection. You will have to read how exactly the HTTP Header are sent and rebuild in part what cUrl already does.

http://www.php.net/fsockopen

 

If you can use a database and cUrl on the server you receive the data you can just append the data in a .CSV or .XML outside the public html and make a little php script that accept login and send the file when you have the right username/password to the other server.

 

Assuming you don't use https to transfers the data, it will always be sent in plaintext so you don't are really secure. You can sent a email to the other server and read it with a CRON/PHP script and put it in a database there.

 

There probably dozen of others way you can do to transfer data, but none more that i can think of right now.

Thanks for all the advise theonlydark.

 

I actually finally got the curl installed so I'm testing it but getting an error message when posting with curl:

 

SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

 

My code is very similar to what you posted, except that I added an If stmt to check for any errors since the data was not going through.

 

Also, a question about the data passed ... In your example you have:

 

<?php $post = "field1=test&field2=something"; ?>

 

would smth like this work also?

 

<?php $post = "field1=t$_POST['first_name'];&field2=$_POST['last_name'];"; ?>

 

Thanks

 

I don't really know enough of cUrl with SSL to help you but if you use that :

 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

 

You can deactiave the SSL check, but there no real point in using a secure connection and not verify it...so i will let others or google answer you.

 

And yes this will work  :

$post = "field1=".$_POST['first_name']."&field2=".$_POST['last_name'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.