开发者

Php Form - Error Checking and output

I have created a simple form which has some required fields which if not completed will pass back an error to the user to inform them that the field is require. As there are multiple field which is checks it can output multiple error messages.

I want to know how I can define an area on my form where these errors are displayed as at the moment these error simply display at the bottom of the form, which you can't see unless you scroll down the page.

Can I define where my error are displayed.

Here is the error checking code: EDIT

  Old code was here

People in the past have suggested I make a loop to check for errors one at a time but I am novice at php so not sure how to do this.

    $errors = '';
    if(empty($_POST['studentName'])) 
    {
    $errors .= "You did not enter the student name<br/>";
    }

//Code to check that the Tutor Name field is completed
    if(empty($_POST['tutorName'] ))
    {
    $errors .="You did not select a tutor<br/>";
    }

//Code to check that the Procedure field is completed
    if(empty($_POST['procedure'] ))
    {
    $errors .="You did not enter a procedure<br/>";
    }
//Code to check that the Grade field is completed
    if(empty($_POST['grade'] ))
    {
    $errors .="You did not enter a grade<br/>";
    }
//Code to check that the Student Reflection field is completed
    if(empty($_POST['studentReflection'] ))
    {
    $errors .="You did n开发者_如何学Pythonot enter a reflection<br/>";
    }
//Code to check if the tick box is checked that the tutor comment is entered
    if( !strlen($_POST['tutorComments']) && isset($_POST['alert'] ))
    {
    $errors .="You must enter a reasan why you ticked the alert box";
    }

//Code to check the password field is completed and correct
    if (empty($_POST['password']))
    {
    $errors .="You did not enter you password";
    }

    if (!empty($_POST['password']))
    {

//==========================================
//  ESCAPE DANGEROUS SQL CHARACTERS
//==========================================
function quote_smart($value, $handle) {

   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }

   if (!is_numeric($value)) {
       $value = "'" . mysql_real_escape_string($value, $handle) . "'";
   }
   return $value;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

    $masterpass = $_POST['password'];
    $masterpass = htmlspecialchars($masterpass);

    //==========================================
    //  CONNECT TO THE LOCAL DATABASE
    //==========================================
    $user_name = "username";
    $pass_word = "password";
    $database = "name of database";
    $server = "server";

    $db_handle = mysql_connect($server, $user_name, $pass_word);
    $db_found = mysql_select_db($database, $db_handle);

    if ($db_found) {

        $masterpass = quote_smart($masterpass, $db_handle);

        $SQL = "SELECT * FROM masterpass WHERE password = $masterpass";
        $result = mysql_query($SQL);
        $num_rows = mysql_num_rows($result);

    //====================================================
    //  CHECK TO SEE IF THE $result VARIABLE IS TRUE
    //====================================================

        if ($result) {
            if ($num_rows > 0) {
                echo "";
            }
            else {
                $errors .= "Password was not recognised";
                exit();
            }   
        }
        mysql_close($db_handle);

    }
}   
if(!empty($errors))
{
    echo '<div class="errors">' . $errors . '</div>';
    exit();
    }   

}


You could do something like

$errors = '';

if(empty($_POST['studentName'])) 
{
  $errors .= "You did not enter the student name<br />";
}

if(empty($_POST['tutorName'] ))
{
  $errors .= "You did not select a tutor name.<br />";
}

// etc.

and then above your <form> have

if (!empty($errors))
{
  echo '<div class="errors">' . $errors . '</div>';
}

styling .errors with CSS so it'll stand out more. If $errors is blank higher up in your application logic, you can then perform the usual add / update to a database and redirect to a success page.


echo()ing your errors is what's adding them to the bottom. As the previous answer suggested, assigning them to a string and printing that in a defined div (if it's not empty) is how the pros do it!

Defining errors as an array also works, and allows you a bit more fine-grained control over the error process.

$errors = array();
if(empty($_POST['studentName'])) 
    $errors[] = "You did not enter the student name";

if(empty($_POST['tutorName'] ))
    $errors[] = "You did not select a tutor name.";

//etc...

//At the top of your page.
if (sizeof($errors)>0) {
    echo "<div class='errordiv'>";
    echo "<ul>";
    foreach ($errors as $err) {
        echo "<li>".$err."</li>"; //or whatever format you want!
    }
    echo "</ul></div>";
}

You can also pass the error array around as a parameter to other functions, log them, etc.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜