當用戶提交表單時,所有POST數據都會發送到salesforce。我目前正在改進服務器上的驗證過程。PHP驗證錯誤消息訂單
當用戶錯過了所有的字段時,驗證過程會挑選公司名稱作爲錯誤消息。
無論如何要放置驗證是爲了例如如果沒有填寫任何字段,則第一個名稱是顯示的第一個錯誤消息。
請查看下面的代碼。
的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);
}
};
?>
該解決方案仍然只會顯示一個(第一個)錯誤 – Tokk
您可以將$ error作爲數組並在驗證代碼後打印此變量。 –