开发者

Php Form - Error Checking

I have created a form online and when the user clicks submit I want the form to check for error (ie missing field). At the moment I have the form checking the fields one by one and as soon as it encounters a error it will exit without checking the rest of the fields. Is there any way I can combine all the if statements that check for errors into one.

Here is the code

 //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 tha开发者_Python百科t 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();
    }


For example, you can make a boolean variable to mark, if there is an error, and exit if it's true + combine error messages into one

$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();
}


There are many ways. How about something like this, from top of my head:

$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
}


Put all your error messages into an array, and loop through the $_POST. If the input field is empty, then echo the error message:

<?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();
}


This can be done like that (one of the many ways -- really depends on your exact requirements for validation):

<?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


Make a variable named $status for example and and initialize it to 0, at each test assign to it 1 if there is an error, at the end check whether if it is equal to one, exit the script otherwise continue the execution. Or better make an array and for each test assign 0 or 1, depend in the test(the field is not empty assign one else zero) and later you can echo an error message to user indicating the missing fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜