2011-06-24 51 views
1

我已經創建了一個在線表單,當用戶點擊提交時,我希望表單檢查錯誤(即缺少字段)。目前,我有表單逐個檢查字段,一旦遇到錯誤,它將退出而不檢查其餘字段。有什麼辦法可以將檢查錯誤的所有if語句合併爲一個。PHP表單 - 錯誤檢查

下面是代碼

//Code to check that the Student Name field is completed 
    if(empty($_POST['studentName'])) 
    { 
    $studentNameError = "You did not enter the student name Wank"; 
    //echo "<h3> $studentNameError </h3>"; 
    exit(); 
    } 
    //Code to check that the Tutor Name field is completed 
    if(empty($_POST['tutorName'])) 
    { 
    echo "<h3>You did not select a tutor name. Please go back and select your name from the tutors list</h3>"; 
    exit(); 
    } 
    //Code to check that the Procedure field is completed 
    if(empty($_POST['procedure'])) 
    { 
    echo "<h3>You did not select a procedure. Please go back and enter the name of the procedure which you undertook</h3>"; 
    exit(); 
    } 
    //Code to check that the Grade field is completed 
    if(empty($_POST['grade'])) 
    { 
    echo "<h3>You did not select a grade. Please go back and select your grade from the drop down list</h3>"; 
    exit(); 
    } 
    //Code to check that the Student Reflection field is completed 
    if(empty($_POST['studentReflection'])) 
    { 
    echo "<h3>The student did not enter any comments for this procedure. Student reflection is required for each procedure. Please go back and enter any comments</h3>"; 
    exit(); 
    } 
    //Code to check if the tick box is checked that the tutor comment is entered 

    if(!strlen($_POST['tutorComments']) && isset($_POST['alert'])) 
    { 
     echo "<h3>You must enter a reason why you have clicked the alert box</h3>"; 
     exit(); 
    } 

回答

0

例如,你可以做一個布爾變量來標記,如果有錯誤,並退出如果這是真的+錯誤訊息合二爲一

$error = false; 
if(empty($_POST['studentName'])) 
{ 
$errorMessages[] = "You did not enter the student name Wank"; 
$error = true; 
} 
//Code to check that the Tutor Name field is completed 
if(empty($_POST['tutorName'])) 
{ 
$errorMessages[] = "You did not select a tutor name. Please go back and select your name from the tutors list"; 
$error = true; 
} 
//Code to check that the Procedure field is completed 
if(empty($_POST['procedure'])) 
{ 
$errorMessages[] = "You did not select a procedure. Please go back and enter the name of the  procedure which you undertook"; 
$error = true; 
} 
//Code to check that the Grade field is completed 
if(empty($_POST['grade'])) 
{ 
$errorMessages[] ="You did not select a grade. Please go back and select your grade from the drop down list"; 
$error = true; 
} 
//Code to check that the Student Reflection field is completed 
if(empty($_POST['studentReflection'])) 
{ 
$errorMessages[] = "The student did not enter any comments for this procedure. Student reflection is required for each procedure. Please go back and enter any comments"; 
$error = true; 
} 
//Code to check if the tick box is checked that the tutor comment is entered 

if(!strlen($_POST['tutorComments']) && isset($_POST['alert'])) 
{ 
    $errorMessages[] = "You must enter a reason why you have clicked the alert box"; 
    $error = true; 
} 
if($error) 
{ 
    echo("<h3>".implode('<br/>',$errorMessages)."</h3>"); 
    exit(); 
} 
+0

非常感謝,似乎太工作 – Mattrsa

0

有很多方法。怎麼樣這樣的事情,從我的頭頂:

$textFieldsThatCannotBeEmpty = array(
    'studentName' => 'You did not enter the student name Wank', 
    'tutorName' => 'You did not select a tutor name. Please go back and select your name from the tutors list', 
    'procedure' => 'You did not select a procedure. Please go back and enter the name of the procedure which you undertook', 
    'grade' => 'You did not select a grade. Please go back and select your grade from the drop down list', 
    'studentReflection' => 'The student did not enter any comments for this procedure. Student reflection is required for each procedure. Please go back and enter any comments' 
); 

$errors = array(); 
// check text input fields 
foreach($textFieldsThatCannotBeEmpty as $name => $errorMessage){ 
    if(empty($_POST[$name])){ 
     $errors[] = $errorMessage; 
    } 
} 
// checkbox 
if(!strlen($_POST['tutorComments']) && isset($_POST['alert'])){ 
    $errors[] = 'You must enter a reason why you have clicked the alert box'; 
} 

if(count($errors) > 0){ 
    // form is invalid, print errors 
    echo '<div class="errors">'; 
    foreach($errors as $e){ 
     echo '<h3>',htmlentities($e),'</h3>'; 
    } 
    echo '</div>'; 
}else{ 
    // form is valid, let's go and do something with the submitted data 
} 
+0

沒關係,但爲什麼你需要預定義的消息htmentities? – Greenisha

+0

@Greenisha只是一個安全相關的習慣。 –

0

把所有的錯誤信息到一個數組中,並遍歷$ _POST。如果輸入字段爲空,然後回顯錯誤消息:

<?php 
$errorMsgs = array(
    'studentName' => 'You did not enter a student name', 
    ... 
); 

$errors = ''; 

foreach($_POST as $field) 
{ 
    if(empty($field)) 
    { 
    $errors .= $errorMsgs[$field] . '<br/>'; 
    } 
} 

if(strlen($errors)) 
{ 
    echo $errors; 
    exit(); 
} 
0

這可以這樣做(的衆多方式之一 - 實際上取決於對驗證您的具體要求):

<?php 
$messages = array(); 
$errors = 0; 

if (empty($_POST['studentName'])) 
{ 
    $messages['studentName'] = "You did not enter the student name Wank"; 
    $errors++; 
} 
if (empty($_POST['tutorName'])) 
{ 
    $messages['tutorName'] = "<h3>You did not select a tutor name. Please go back and select your name from the tutors list</h3>"; 
    $errors++; 
} 

if ($errors) { 
    // we have some invalid data in one of the fields 
    // display error messages (some feedback to user) 
    foreach ($messages as $v) { 
     echo $v, "\n"; 
    } 
    exit(); 
} 

// nope, we are fine 
// do whatever else is required 
0

例如,創建一個名爲$status的變量,並將其初始化爲0,在每次測試時分配給它1,如果有錯誤,最後檢查它是否等於1,退出腳本,否則繼續執行。或者更好地創建一個數組,併爲每個測試分配0或1,依賴於測試(該字段不爲空分配另一個零),然後您可以向用戶回顯錯誤消息,指出缺少的字段。