-2

这是由 TonyMancini 组建的,但我现在需要帮助。表单确实验证了,但是,它出现了一个新页面,看起来像

您提交的内容有 57 个错误,请更正。

请输入您的名字

您提交的内容有 57 个错误,请更正。

请输入您的姓氏

您提交的内容有 57 个错误,请更正。

请输入您的 9 位社会保险号

您提交的内容有 57 个错误,请更正。

请输入您的电话号码

您提交的内容有 57 个错误,请更正。

请输入您的地址

您提交的内容有 57 个错误,请更正。

请输入您的城市

等等

是否可以在我的表单字段旁边显示错误?或不?

<?php
// Create a dynamic array for the error messages and validate everything before sending them back to the form that shows them
// Here is a sample of your code with the dynamic array built in

$error_msgs = array ();
$error = 0; //Error flag

if ($_POST['first_name']){
$first_name = $_POST['first_name'];
} else {
$error_msgs[] = "Please enter your first name";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['last_name']){
$last_name = $_POST['last_name'];
} else {
$error_msgs[] = "Please enter your last name";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ssn']){
$ssn = $_POST['ssn'];
} else {
$error_msgs[] = "Please enter your 9 digit social security number";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['phone']){
$phone = $_POST['phone'];
} else {
$error_msgs[] = "Please enter your phone number";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['address']){
$address = $_POST['address'];
} else {
$error_msgs[] = "Please enter your address";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['city']){
$city = $_POST['city'];
} else {
$error_msgs[] = "Please enter your city";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['state']){
$state = $_POST['state'];
} else {
$error_msgs[] = "Please select your state";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['zip']){
$zip = $_POST['zip'];
} else {
$error_msgs[] = "Please enter your 5 digit zip code";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['position']){
$position = $_POST['position'];
} else {
$error_msgs[] = "Please enter position desired, or type N/A if open for discussion";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['wage']){
$wage = $_POST['wage'];
} else {
$error_msgs[] = "Please enter 0.00 if open for discussion";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['Emergencycontactname']){
$Emergencycontactname = $_POST['Emergencycontactname'];
} else {
 $error_msgs[] = "Please enter your emergency contact's name";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['Emergencycontactaddress']){
$Emergencycontactaddress = $_POST['Emergencycontactaddress'];
} else {
 $error_msgs[] = "Please enter your emergency contact's address";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['Emergencycontactcity']){
$Emergencycontactcity = $_POST['Emergencycontactcity'];
} else {
 $error_msgs[] = "Please enter your emergency contact's city";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['Emergencycontactstate']){
$Emergencycontactstate = $_POST['Emergencycontactstate'];
} else {
 $error_msgs[] = "Please select your emergency contact's state";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['Emergencycontactzip']){
$Emergencycontactzip = $_POST['Emergencycontactzip'];
} else {
 $error_msgs[] = "Please enter your emergency contact's 5 digit zip code";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['Emergencycontactphone']){
$Emergencycontactphone = $_POST['Emergencycontactphone'];
} else {
 $error_msgs[] = "Please enter your emergency contact's phone number";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['highest']){
$highest = $_POST['highest'];
} else {
 $error_msgs[] = "Please enter your highest level of education";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['degree']){
$degree = $_POST['degree'];
} else {
 $error_msgs[] = "Please enter N/A if none";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['age']){
$age = $_POST['age'];
} else {
 $error_msgs[] = "Please enter yes or no";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['problems']){
$problems = $_POST['problems'];
} else {
 $error_msgs[] = "Please enter yes or no";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['shiftwork']){
$shiftwork = $_POST['shiftwork'];
} else {
 $error_msgs[] = "Please enter yes or no";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['apprenticeship']){
$apprenticeship = $_POST['apprenticeship'];
} else {
 $error_msgs[] = "Please enter yes or no";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['disabilities']){
$disabilities = $_POST['disabilities'];
} else {
 $error_msgs[] = "Please enter yes or no";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ifyes']){
$ifyes = $_POST['ifyes'];
} else {
 $error_msgs[] = "If you answered no to above question, type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['accommodate']){
$accommodate = $_POST['accommodate'];
} else {
 $error_msgs[] = "Type N/A if you answered N/A for above question";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['experience']){
$experience = $_POST['experience'];
} else {
 $error_msgs[] = "Please enter yes or no";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ifyes2']){
$ifyes2 = $_POST['ifyes2'];
} else {
 $error_msgs[] = "If you answered no to above, type N/A, otherwise describe";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cname']){
$cname = $_POST['cname'];
} else {
 $error_msgs[] = "Please enter company name";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['caddress']){
$caddress = $_POST['caddress'];
} else {
 $error_msgs[] = "Please enter company address";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cphone']){
$cphone = $_POST['cphone'];
} else {
 $error_msgs[] = "Please enter company phone number";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ccperson']){
$ccperson = $_POST['ccperson'];
} else {
 $error_msgs[] = "Please enter company contact person's name";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ctype']){
$ctype = $_POST['ctype'];
} else {
 $error_msgs[] = "Please enter type of work";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cwage']){
$cwage = $_POST['cwage'];
} else {
 $error_msgs[] = "Please enter wage earned";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cstart']){
$cstart = $POST['cstart'];
} else {
 $error_msgs[] = "Please enter date started";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cend']){
$cend = $POST['cend'];
} else {
 $error_msgs[] = "Please enter date ended";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['reason']){
$reason = $_POST['reason'];
} else {
 $error_msgs[] = "Please enter reason for leaving";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cname2']){
$cname2 = $_POST['cname2'];
} else {
 $error_msgs[] = "Please enter company name or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['caddress2']){
$caddress2 = $_POST['caddress2'];
} else {
 $error_msgs[] = "Please enter company address or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cphone2']){
$cphone2 = $_POST['cphone2'];
} else {
 $error_msgs[] = "Please enter company phone number or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ccperson2']){
$ccperson2 = $_POST['ccperson2'];
} else {
 $error_msgs[] = "Please enter company contact person's name or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ctype2']){
$ctype2 = $_POST['ctype2'];
} else {
 $error_msgs[] = "Please enter type of work or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cwage2']){
$cwage2 = $_POST['cwage2'];
} else {
 $error_msgs[] = "Please enter wage earned or type 0.00 if N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cstart2']){
$cstart2 = $POST['cstart2'];
} else {
 $error_msgs[] = "Please enter date started or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cend2']){
$cend2 = $POST['cend2'];
} else {
 $error_msgs[] = "Please enter date ended or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['reason2']){
$reason2 = $_POST['reason2'];
} else {
 $error_msgs[] = "Please enter reason for leaving or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cname3']){
$cname3 = $_POST['cname3'];
} else {
 $error_msgs[] = "Please enter company name or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['caddress3']){
$caddress3 = $_POST['caddress3'];
} else {
 $error_msgs[] = "Please enter company address or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cphone3']){
$cphone3 = $_POST['cphone3'];
} else {
 $error_msgs[] = "Please enter company phone number or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ccperson3']){
$ccperson3 = $_POST['ccperson3'];
} else {
 $error_msgs[] = "Please enter company contact person's name or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['ctype3']){
$ctype3 = $_POST['ctype3'];
} else {
 $error_msgs[] = "Please enter type of work or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cwage3']){
$cwage3 = $_POST['cwage3'];
} else {
$error_msgs[] = "Please enter wage earned or type 0.00 if N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cstart3']){
$cstart3 = $POST['cstart3'];
} else {
$error_msgs[] = "Please enter date started or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['cend3']){
$cend3 = $POST['cend3'];
} else {
$error_msgs[] = "Please enter date ended or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['reason3']){
$reason3 = $_POST['reason3'];
} else {
$error_msgs[] = "Please enter reason for leaving or type N/A";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['maywe']){
$maywe = $_POST['maywe'];
} else {
$error_msgs[] = "Type names of companies you do not want us to contact, or type, ok all";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['additional']){
$additional = $_POST['additional'];
} else {
$error_msgs[] = "Type N/A if you do not have any additional information";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['electronicsignfn']){
$electronicsignfn = $_POST['electronicsignfn'];
} else {
$error_msgs[] = "Please enter your full name";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['electronicsignssn']){
$electronicsignssn = $_POST['electronicsignssn'];
} else {
$error_msgs[] = "Please enter your 9 digit social security number";
$errors++; //Flag to let the code know that there are errors, used in every case
}

if ($_POST['emailaddress']){
$emailaddress = $_POST['emailaddress'];
} else {
$error_msgs[] = "Please enter your email address";
$errors++; //Flag to let the code know that there are errors, used in every case
}

// Finally at the end, check to see if there are any errors and display them all
if ($errors > 0) {
// Display your error array
foreach (    $error_msgs as &$msg) {
echo "There were " . $errors . " errors in your submission, please correct them.<br/>";
echo $msg . "<br/>";
}
} else {
// Add code here to submit the form however you'd like
}
?>
4

1 回答 1

0

您的条件测试很可能每次都会评估为假。您可能需要像这样使用 isset() 或 empty() 进行测试:

if (isset($_POST['field'])) {
    // a value was posted
} else {
    // error
}

或者

if (!empty($_POST['field'])) {
    // a value was posted
} else {
    // error
}

错误可能会显示在您的输入字段旁边,但您需要首先处理第一件事,即让您的表单正常工作。

于 2012-08-07T17:57:33.387 回答