Reputation: 4566
I am trying to implement a validation process within the php wrapper for the new web to lead form (salesforce).
The lead gets submitted automatically and completely ignores the validation process.
if (empty($_POST["first_name"]))
{$firstNameErr = "Name is required";}
else
{$first_name = test_input($_POST["first_name"]);}
if (empty($_POST["last_name"]))
{$lastNameErr = "Email is required";}
else
{$last_name = test_input($_POST["last_name"]);}
if (empty($_POST["phone"]))
{$phoneErr = "";}
else
{$phone = test_input($_POST["phone"]);}
if (empty($_POST["email"]))
{$emailErr = "";}
else
{$email = test_input($_POST["email"]);}
if (empty($_POST["company"]))
{$companyNameErr = "Gender is required";}
else
{$company = test_input($_POST["company"]);}
Here is the full php code:
<?php
//Initialize the $query_string variable for later use
$query_string = "";
$firstNameErr = $lastNameErr = $phoneErr = $emailErr = $companyNameErr = "";
$first_name = $last_name = $phone = $email = $company = "";
//If there are POST variables
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);
if (empty($_POST["first_name"]))
{$firstNameErr = "Name is required";}
else
{$first_name = test_input($_POST["first_name"]);}
if (empty($_POST["last_name"]))
{$lastNameErr = "Email is required";}
else
{$last_name = test_input($_POST["last_name"]);}
if (empty($_POST["phone"]))
{$phoneErr = "";}
else
{$phone = test_input($_POST["phone"]);}
if (empty($_POST["email"]))
{$emailErr = "";}
else
{$email = test_input($_POST["email"]);}
if (empty($_POST["company"]))
{$companyNameErr = "Gender is required";}
else
{$company = test_input($_POST["company"]);}
}
//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);
?>
Upvotes: 0
Views: 1051
Reputation: 4566
Well, completely redeveloped a framework that allows me to do such a thing.
If anyone wants a salesforce web2lead which can easily be connected with any type of form:
https://github.com/jagmitg/Salesforce-Web2lead
Upvotes: 0