Check if multiple form fields are empty

Tutorials for php.

Check if multiple form fields are empty

Postby Mortisimus on Mon Aug 25, 2008 6:33 pm

Hello. In this tutorial, we will write a function that will error check multiple variables submitted from a form. First, I'll give you an example form, we will use the fields in this form for the example code.

form.php

Code: Select all
<form action="parse.php" method="post">
Field 1:
<input type="text" name="field1" size="30" />
Field 2:
<input type="text" name="field2" size="30" />
<input type="submit" name="submit" value="Check" />
</form>


Right, so we have the form, lets write the function. The form's action is parse.php so create a new file called parse.php.

parse.php

Code: Select all
function checkFields($input=array()) {
    // checking code goes here
}


So, basically, we have created a function called checkFields. This has 1 parameter, which is the variable $input (when you actually use the function, the variable for the parameter does not actually have to be called $input) and that is parsed as an array.

Now, we will add the code that checks it. Firstly, we need to create a variable to get the amount of keys in the inputted array. For this, we will use the count() function. Also, we will add a variable to hold an error if we have one and a variable just to check if there is an error.

Code: Select all
$error = "";
$fields = count($input);
$problem = FALSE;


Next, we will have to use a for loop to go through each element in the array and check if it's empty.

Code: Select all
for($i=0; $i<$fields; $i++){
    if(empty($fields[$i])){
        //the current field being checked is empty
        $error = "One or more of the fields you entered were empty.\n";
        $problem = TRUE;
    }
}


What the code above does is checks through every element in the array until all of them have been checked. Then, the if statement checks if the value of the element corresponding to the current array key that $i is equal to at this point in the loop is empty. If it is empty, the $error variable is assinged a string to display. The $problem variable is set to true, this is used in the next part of the function.

Under the for loop, add the following code:

Code: Select all
if($problem){
    echo $error;
    return FALSE;
}
else
{
    return TRUE;
}

All this does is check if the $problem variable's value is true. If it is, the error will be displayed and the function will return false. Otherwise, if there is no errors, the function will return true.

So, here is an example of how to use this function. The example is still using the form that we wrote at the start. This is an example parse.php.

Code: Select all
//firstly, we need the function in here.
function checkFields($input=array()) {

    $error = "";
    $fields = count($input);
    $problem = FALSE;

    for($i=0; $i<$fields; $i++){
        if(empty($fields[$i])){
            //the current field being checked is empty
            $error = "One or more of the fields you entered were empty.\n";
            $problem = TRUE;
        }
    }

    if($problem){
        echo $error;
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
//end the function, parse it
$fields = array($_POST['field1'], $_POST['field2']);

$check = checkFields($fields);

//if the function returns true, there is an error, so...
if($check){
    //there will be an error here, but the error message will already be displayed
    //so you dont have to put anything in here
}
else
{
    //there is no error here, continue the script for whatever you're doing.
}


This is just a quick way of error checking forms. Thankyou for reading.
User avatar
Mortisimus
Site Admin
 
Posts: 21
Joined: Mon Aug 25, 2008 6:04 pm

Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests

cron