我在使用之前的开发人员创建的表单时遇到了一些问题。当用户填写我们的表单时,它不会提醒用户必填字段不完整,只是将用户重定向到“谢谢”页面。由于缺少必填字段,表单未送达。
任何人都可以通过代码告诉我们在缺少必填字段时应该做些什么吗?理想情况下,我希望它突出显示缺少的字段,以便用户可以输入所需的信息。如果它从来没有被设计为首先这样做,我希望它能传递信息,不管是否缺少信息,而我找出必要的代码,使其突出显示缺少的字段。
问题代码:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "eileenw@ourdomain.com";
$email_subject = "Quiz";
function died($error) {
// your error code can go here
echo "<p>We are very sorry, but there were error(s) found with the form you submitted.</p> ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "<p>Please go back and fix these errors.<br /><br /></p>";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$address = $_POST['address']; // not required
$city = $_POST['city']; // not required
$state = $_POST['state']; // not required
$vehicleyear = $_POST['vehicleyear']; // not required
$vehiclemake = $_POST['vehiclemake']; // not required
$vehiclemodel = $_POST['vehiclemodel']; // not required
$purchase_or_lease = $_POST['purchase_or_lease']; // not required
$deliverydate = $_POST['deliverydate']; // not required
$mileage_at_delivery = $_POST['mileage_at_delivery']; // not required
$current_mileage = $_POST['current_mileage']; // not required
$seller = $_POST['seller']; // not required
$citystate = $_POST['citystate']; // not required
$Bank_Finance = $_POST['Bank_Finance']; // not required
$dealer_arranged = $_POST['dealer_arranged']; // not required
$employee_discount = $_POST['employee_discount']; // not required
$warranty = $_POST['warranty']; // not required
$length_contract = $_POST['length_contract']; // not required
$comments = $_POST['comments']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Vehicle Year: ".clean_string($vehicleyear)."\n";
$email_message .= "Make: ".clean_string($vehiclemake)."\n";
$email_message .= "Model: ".clean_string($vehiclemodel)."\n";
$email_message .= "Purchase or Lease: ".clean_string($purchase_or_lease)."\n";
$email_message .= "Delivery date: ".clean_string($deliverydate)."\n";
$email_message .= "Mileage at Delivery: ".clean_string($mileage_at_delivery)."\n";
$email_message .= "Current Mileage: ".clean_string($current_mileage)."\n";
$email_message .= "Selling Dealer: ".clean_string($seller)."\n";
$email_message .= "Seller City and State: ".clean_string($citystate)."\n";
$email_message .= "Bank or Finance Company: ".clean_string($Bank_Finance)."\n";
$email_message .= "Dealer Arranged: ".clean_string($dealer_arranged)."\n";
$email_message .= "Employee Discount? ".clean_string($employee_discount)."\n";
$email_message .= "Warranty: ".clean_string($warranty)."\n";
$email_message .= "Contract Terms: ".clean_string($length_contract)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: info@ourdomain.com' . "\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
答案 0 :(得分:1)
表单并不完整,因为您检查它的方式是错误的。 isset($_POST['foo'])
不会查看该字段是否有值,只会检查该字段是否已提交。
e.g。
<input type="text" name="foo" value="" />
将提交foo=
。密钥存在,因此isset($_POST['foo'])
将为true
。它只有一个空字符串作为其值。如果该字段存在且它包含某些内容,您需要检查BOTH,例如
if ($_POST['foo'] === '') {
blank field submitted
} else {
something got submitted
}
答案 1 :(得分:0)
您需要将required
字段添加到html输入标记中。这将提醒用户在单击提交按钮时需要填写该字段。
这是一个例子
<form action="form.php">
FirstName: <input type="text" name="FirstName" required>
<input type="submit">
</form>