2011-06-24 6 views
1

私はオンラインでフォームを作成しました。ユーザーがsubmitをクリックすると、フォームでエラー(つまり、フィールドがない)をチェックします。現時点では、フィールドを1つずつチェックするフォームがあり、エラーが発生するとすぐにフィールドの残りの部分をチェックせずに終了します。エラーをチェックするif文をすべて1つにまとめる方法はありますか?ここで 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

たとえば、エラー、および終了がある場合には、1

$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

(多くの方法のうちの1つを - 実際に検証のためのあなたの正確な要件に依存します):

<?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を割り当て、テストに依存します(フィールドは空ではありません)。

関連する問題