PHP Function to Check Form Fields

PHP Function to Check Form Fields

Easily check form fields with PHP Function

There's an open issue on GitHub specifying that this PHP function, does not work well with Ajax as an API.

I got an unexpected token error, yesterday as I tried to use it as an API using Ajax.

form-fields-issue.PNG

Today, I have issued a fix for it and the updated function is on gitHub

EXPLANATION

I have simplified the function to accept just 2 arguments

  • $fields which is the array holding the fields to check and their respective field tags
  • $dump which is a boolean (true or false). This will enable the function to output the status and empty fields after the check has completed.

Now, whether you're using an API or calling the function directly, you must set $dump to true if you want to capture the status of the check and alert users that some fields are empty.

You can call the function like so;

<?php
 $empty = json_decode(checkFormFields($fields, true), true);

        if ($empty["status"] != 1){

       //Form processing here

        }else {
         echo "Some fields are missing";
        }
?>

Some ways to access the function's output;

// get the status of the check
echo $empty["status"]; 

// get the empty fields
print_r($empty['fields']);

//Alert user that some fields are empty
foreach ($empty['fields'] as $index => $field) {

      echo $field . 'is required' . '<br>';

}

//Let user know the number of fields empty
echo count($empty['fields']);

I find using this function a very convenient method if you are working with PHP form submissions.

This will save you a lot of space and time when you are checking your form fields like this ;

if(isset($_POST) && isset($_POST['fname']) && isset($_POST['mname']) && isset($_POST['email']))
{
    //something magical happens here
}

And will also save you the stress of trying to figure out the fields that are empty so that you can alert the user.

if(isset($_POST) && !isset($_POST['fname']) 
{
 echo "First Name is required!";
}

Now I can close the issue on GitHub.

If you have any questions or suggestions, feel free to drop a comment below.

You can visit this Script on GitHub by following the link below;

Visit on GitHub

HOW TO USE IT

  • Download the script from Github.

  • Import the script to your PHP page.

<?php
require_once 'PHP-Snippets/forms/form-fields-v1.3.php';
?>
  • Set up your Fields ARRAY with it's associated field tags too (the field tag is what we will show the user when the field is missing).
<?php
require_once 'PHP-Snippets/forms/form-fields-v1.3.php';

$fields = [
"fname" => "First Name",
"lname" => "Last Name",
"email" => "Email Address"
 ];

?>
  • Call the function by binding it to a variable, then use json_decode to handle the output.
<?php
require_once 'PHP-Snippets/forms/form-fields-v1.3.php';

$fields = [
"fname" => "First Name",
"lname" => "Last Name",
"email" => "Email Address"
 ];

//call the function
$empty = json_decode(checkFormFields($fields, true), true);
?>
  • Access function output
<?php
require_once 'PHP-Snippets/forms/form-fields-v1.3.php';

$fields = [
"fname" => "First Name",
"lname" => "Last Name",
"email" => "Email Address"
 ];

//call the function
$empty = json_decode(checkFormFields($fields, true), true);

//If status is 0, you're clear
if ($empty["status"] != 1){
  //Form processing here
}else {
    //If status is 1, there're empty fields
    if($empty["status"] == 1){
        //Output empty fields to user
        foreach ($empty['fields'] as $index => $field) {
            echo $field .'is required'.'<br>';
        }
    } 
}
?>

If the status returned is 1, it means that some fields are missing a value and you can access those fields. But if the status returned is 0, it means that no field is missing a value. So you can check if the status returned by the function is 1 or 0.

Thats All!

  • Other actions you can execute
<?php
//Let user know the number of fields empty
echo count($empty['fields']);
?>

You can use JavaScript to handle the Function's data if you are working with API.

Thank You!