When a user submits the form, all the POST data gets sent across to salesforce. I am currently working on improving the validation process on the server.
When the user miss out the all the fields, the validation process picks up company name as the error message.
Is there anyway to place the validation in order e.g. if none of the fields are filled out, first name is the first error message shown.
Please review the code below.
index.php
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
<input type='hidden' name="lead_source" value="web-to-lead">
<input type='hidden' name="Campaign_ID" value="campaignid">
<input type='hidden' name="oid" value="uniqueid">
<input type='hidden' name="ididid" value="Grade 3 (Hot)">
<input type='hidden' name="retURL" value="www.niceic.com">
<input type="hidden" id="recordType" name="recordType" value="ididididid">
<input type='hidden' name="designgenieid" value="Design Genie Webinar">
<div class="errorMessage"><?php if(isset($error)){ echo $error; } ?></div>
<label for="first_name">First Name</label>
<input id="first_name" name="first_name" size="20" type="text" value="<?php echo $_POST['first_name']; ?>" /><br>
<label for="last_name">Last Name</label>
<input id="last_name" name="last_name" size="20" type="text" value="<?php echo $_POST['last_name']; ?>" /><br>
<label for="phone">Phone</label>
<input id="phone" name="phone" size="20" type="text" value="<?php echo $_POST['phone']; ?>" /><br>
<label for="email">Email</label>
<input id="email" name="email" size="20" type="text" value="<?php echo $_POST['email']; ?>" /><br>
<label for="company">Company</label>
<input id="company" name="company" size="20" type="text" value="<?php echo $_POST['company']; ?>" /><br>
<label for="00ND0000005gYZo">Question<input id="00ND0000005gYZo" maxlength="80" name="00ND0000005gYZo" value="<?php echo $_POST['00ND0000005gYZo']; ?>" type="text">
<br><input type="submit" name="submit">
</form>
config.php
<?php
//Initialize the $query_string variable for later use
$query_string = "";
if(isset($_POST['submit'])){
if ($_POST['first_name'] == "") {
$error="Please enter in your first name<br>";
} elseif (!preg_match("/^[a-zA-Z ]*$/", $_POST['first_name'])) {
$error="Only letters and white space allowed<br>";
}
if($_POST['last_name'] == ""){
$error="Please enter in your last name<br>";
}elseif(!preg_match("/^[a-zA-Z ]*$/",$_POST['last_name'])){
$error="Only letters and white space allowed<br>";
}
if($_POST['phone'] == ""){
$error="Please enter in your phone number<br>";
}elseif(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $_POST['phone'])){
$error="Please enter in a valid phone number<br>";
}
if($_POST['email'] == ""){
$error="Please enter in your email<br>";
}else if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['email'])){
$error="Please enter in a valid email address<br>";
}
if($_POST['company'] == ""){
$error="Please enter in your Company details<br>";
} elseif (!preg_match("/^[a-zA-Z ]*$/", $_POST['first_name'])) {
$error="Only letters and white space allowed<br>";
}
if(isset($error)){
//echo $error;
}else{
if ($_POST) {
//Initialize the $kv array for later use
$kv = array();
//For each POST variable as $name_of_input_field => $value_of_input_field
foreach ($_POST as $key => $value) {
//Set array element for each POST variable (ie. first_name=Arsham)
$kv[] = stripslashes($key)."=".stripslashes($value);
}
//Create a query string with join function separted by &
$query_string = join("&", $kv);
}
//Check to see if cURL is installed ...
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
//The original form action URL from Step 2 :)
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
//Open cURL connection
$ch = curl_init();
//Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($kv));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
//Set some settings that make it all work :)
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
//Execute SalesForce web to lead PHP cURL
$result = curl_exec($ch);
//close cURL connection
curl_close($ch);
}
};
?>