2012-08-07 44 views
-2

這是formed by 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 
} 
?> 

回答

0

您的條件測試很可能每次都會評估爲false。您可能需要使用isset()函數或空()這樣的測試:

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

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

很可能有錯誤顯示旁邊的輸入字段,但是你需要首先處理第一件事,那就是讓你的表單正常工作。

+0

我的意思是它的效果很好,但錯誤消息顯示在一個新的頁面上,你必須點擊後退按鈕,但是現在你可能會記住你需要修復的錯誤。 – 2012-08-07 17:59:17

+0

我會補充一點,我不是很流利的編程語言,事實上,我吮吸它。 – 2012-08-07 18:02:21

+0

感謝您的幫助邁克 – 2012-08-13 15:18:23