Easily Check FORM Required fields – PHP

Easily Check FORM Required fields – PHP

One Array to check for form required fields

Well, it’s very easy to check if customers filled up your form with the basic HTML required Attribute.

<input type="text" required name="username">

But How do we achieve such in PHP?

Well, Here’s the logic. You have to create 2 arrays. One will hold all required field names and the Other will hold the required fields that the user submitted without a value.

<?php
    //This Array holds all required fields
    $requiredFields = ["username", "email", "phone"];

    // This Array will store required fields without a value
    $requiredFieldsMsg= [];
 ?>

Now we need to loop through the required Fields Array, to find the field without a value, and then push that field to the other array holding the required fields without a value.

<?php
    foreach($requiredFields as $requireField){
    //Check if the form field contains a value
    if(!($_POST[$requireField])) {
    /*
    if it doesn't, push the field to the
     array that will store required fields
     without a value
    */
    array_push($requiredFieldsMsg,$requireField);
    }
    }
 ?>

Now we can check if there are values in the Array holding the required fields without a value. If there are values, alert the user that the field is required, otherwise perform your (form Input Sanitization , Validation and DB insert).

<?php
    /*
    if there're values in the array holding
    the required fields, alert the user
    */
    if (isset($requiredFieldsMsg) && count($requiredFieldsMsg) > 0)
    {
    foreach($requiredFieldsMsg as $key=>$requireFieldMsg)
    {
    echo '<div align="center">
    <div class="notify">
    <p>'.$requireFieldMsg.' field is required!</p>
    </div>
    </div>'; 
    }
    }
    }
    else{
    //YOUR FORM ACTION GOES HERE
    }
?>

Here’s the full code with HTML. Just copy the codes and paste in a blank PHP file.

<?php
    //You can add as many fields as you like
    if($_POST){
    $requiredFields = [
    "username", "email",  "phone"
     ];

    /*
    Initialize the array that will store the
    fields without a value
    */
    $requiredFieldsMsg= [];

    foreach($requiredFields as $requireField){

    //Check if the form field contains a value
    if(!($_POST[$requireField])) {
    //if it doesn't, push the field to the array
      array_push($requiredFieldsMsg,$requireField);
    }
    }

    //check if there're values in the array, then alert the user
    if (isset($requiredFieldsMsg) && count($requiredFieldsMsg) > 0) {
     foreach($requiredFieldsMsg as $requireFieldMsg){
      echo '<div align="center">
      <div class="notify">
      <p>'.$requireFieldMsg.' field is required!
      </p>
      </div>
      </div>' ;
      }
    }
    }
    else
    {
     //YOUR FORM ACTION GOES HERE 
    }
    //End of script
    ?>

    <html>
    <style>
    div.notify{
    max-width:500px;
      text-align:left;
        border-radius: 4px;
        position: relative;
        padding: 1.25rem 2.5rem 1.25rem 1.5rem;
        background-color: #feecf0 !important;
        color: #cc0f35;
        margin-bottom: 10px;
    }
    </style>
    <form method="POST">
    <label>Username</label><br>
    <input type="text" name="username"><br>
    <label>Email</label><br>
    <input type="text" name="email"><br>
    <label>Phone</label><br>
    <input type="text" name="phone"><br>
    <button type="submit">TEST</button>
    </form>
 </html>

In Conclusion

  1. You add the form fields to check in $requiredFields, passing only the name of the form field.
  2. The script loops through the array to check if each of the form fields specified contains a value using $_POST[]
  3. If any field does not contain a value, it will take the name of the field and add it to the other array $requiredFieldsMsg= []
  4. Then it loops through the array $requiredFieldsMsg= [] and alerts the user that the fields are required.

UPDATE

This particular post for this PHP script has been tagged as V1.0

A well structured code for this PHP script has been published in another post and tagged V1.2

Here are the changes;

  • Custom field tags (eg. Email address, phone number, Social Security Number etc) are supported in V1.2 of this script.
  • You can store the function and use it seamlessly in all your form pages.
  • A return value is included. With this you can know if all fields or some fields passed the test.
  • You can modify the "User alert section", if you want to alert the user that some fields are missing a value.

    Here's a link to V1.2 of this script

Thank You !

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