Skip to main content

Command Palette

Search for a command to run...

Form Fields PHP - v1.2

How to check if form fields contains a value (!empty) with PHP .

Updated
•3 min read•View as Markdown
Form Fields PHP - v1.2
S

My name is Simon Ugorji and I aspire to become a computer scientist.

I officially started my web development journey in the year 2020 as a backend web Developer and I have enjoyed the experience so far.

I spend my day coding, learning new topics, and figuring out the next article to share with the world.

I had earlier published a post where you can easily check your form fields server side with PHP and alert the user that some of the fields are empty here.

Well, good news is that the script works just fine but here are some things I noticed on the previous script.

- If you shortened the field name, it gets outputed just like that.

Eg; lname is required, fname is required, num is required instead of last name is required, first name is required, phone number is required.

- There's no return value included, so you have to include the script at the start of your ($_POST) block for it to work just fine.

NOTE

I will be tagging the releases to this PHP script and future updates will be available right on GitHub.

UPDATE 1.2

I have used a function and have included two (2) arguments to the function. You can call the function like this :

//syntax
checkFormFields(fields_array, return);

//call the function

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

checkFormFields($fields, true)

fields_array This will be the array that will hold your form fields that are to be filled up by users.

return This is a Boolean that will specify if the function should return a value .

if the value returned is 1, it means that some fields are missing a value. if the value returned is 0, it means that no fields are missing a value.

So you can check if the value returned by the function is 1 or 0 ;

if (checkFormFields($fields, true) == 1) {
    echo "one or more fields are empty";
} else {
    echo "No field is empty";
}

The return argument must be set to true if you want to use the return value.

FULL SCRIPT

Here's the full script below.

function checkFormFields($fields, $return) {

    $requiredFields = [];  //init required fields var
    $emptyFields = array();  //init empty fields var


  foreach ($fields as $field=>$fieldTag) //loop through the array to find the fields and field tags associated
{

    array_push($requiredFields, $field);  //push required field to the array

} //end of foreach loop

foreach($requiredFields as $requireField) {

     $currentField = $requireField; //store current field
     $currentFieldTag = $fields[$currentField]; //store current field tag

  if (!($_POST[$currentField]))
{
      array_push ($emptyFields, $currentFieldTag); //push the tag name to the empty field

} //end of if field is empty

} //end of check if field has a value

 /***
     * 
     * IF FIELD IS EMPTY BLOCK
     * 
 ***/

if (isset($emptyFields) && count($emptyFields) > 0) //check if there's an empty field in the variable
{ // then loop through

  foreach ($emptyFields as $emptyField)   {

/***
     * 
     *  CUSTOM USER ALERT ACTION GOES HERE 
     *
    ** */

    echo '
       <script>
        document.addEventListener(\'DOMContentLoaded\', function(){
       alert(\''.$emptyField.' is required!\');
        });
       </script>'; //ALERT THE USER 

} //END OF empty fields loop

} //end of check if there's an empty field

//if return is set to true
if($return == true) {
  if (isset($emptyFields) && count($emptyFields) > 0) {
        return(1);
}   else {
        return (0);
} 
}
} //end of function

Before you use it, make sure you have created an empty php page and have added some form fields like;

 <html>
     <form method="post">
         <input name="fname"> <br>
         <input name="lname"> <br>
         <input name="email"> <br>
         <button type="submit">Submit</button>
     </form>
 </html>

Then for server side processing;

if ($_POST){
$fields = [
    "fname" => "First Name",
    "lname" => "Last Name",
    "email" => "Email"
];

//call the function

if (checkFormFields($fields, false) == 1) {
    echo "one or more fields are empty";
} else {
    echo "No field is empty";
}
}

EXTRA

You can still alert the user if any of the fields are missing by modifying the codes in the block

CUSTOM USER ALERT ACTION GOES HERE

Thank You !

If you found it useful or have any other amazing feature to add, drop a comment below!

30 views
S

I noticed yesterday that the PHP function works so well if you're using it directly and not as an API using Ajax.

If a form field is missing a value, it doesn't output the fields missing a value when building as an API with Ajax

You will see some error about an unexpected token

Well this is because it was built only to be used directly from your PHP script, not as an API.

I have taken notice of this bug and will issue a fix soon.

S

If you want to know the exact location of the bug, check the block

CUSTOM USER ALERT