Understanding PHP Form Required Fields
Throughout the article, we will show you PHP form required fields, for both server-side and client-side validation methods.
Also, we’ll discuss some best practices in order for you to create user-friendly, intuitive forms that are easy to complete.
Forms should be designed in a way that makes sure certain fields are mandatory as this will prevent users from entering inaccurate or incomplete information in the form.
PHP Form Required Fields
In previous article, we noted that six validation rules were required.
We need to fill out below fields:
- First Name
- Last Name
- Age
- Designation
- Write your skills
It is mandatory fields and can’t be empty and submitted without being filled.
Fields | Validation Rules |
First name | Required field + There must only be letters and whitespace in the text |
Last name | Required field + There must only be letters and whitespace in the text |
Age | Required field + Valid number greater than 0 |
Required field + The email address must contain an (@ and .) | |
Year of Experience | Optional field + If you have experience, enter a number more than 0 |
Designation | Required field + There must only be letters and whitespace in the text |
Optional field + If have account on linkedIn then enter it | |
Write your Skills | Required field + There must only be letters and whitespace in the text |
There are some new variables in the following code that we have added:
- $fnameErr
- $lnameErr
- $ageErr
- $emailErr
- $designationErr
- $skillsErr
Error messages will be stored in these error variables corresponding to the required fields.
The if else statements have also been added for each of the $_POST variables.
By using the empty() function, we can check if the $_POST variable is empty.
The function input_validation() checks if the input field is empty, and if it is not empty, it sends the user input data to be validated by the input_validation() function otherwise it sends an error message to the error variables:
$fnameErr = $lnameErr = $ageErr = $emailErr = $designationErr = $skillsErr = ""; $firstname = $lastname = $age = $email = $designation = $skills = $linkedin = $yrofexp = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if(empty($_POST("firstname"))){ $fnameErr = "First name is required"; } else{ $firstname = input_validation($_POST["firstname"]); } if(empty($_POST("lastname"))){ $lnameErr = "Last name is required"; } else{ $lastname = input_validation($_POST["lastname"]); } if(empty($_POST("age"))){ $ageErr = "Age is required"; } else{ $age = input_validation($_POST["age"]); } if(empty($_POST("email"))){ $emailErr = "Email is required"; } else{ $email = input_validation($_POST["email"]); } if(empty($_POST("skills"))){ $skillsErr = "Email is required"; } else{ $$skills = input_validation($_POST["skills"]); } if(empty($_POST("designation"))){ $designationErr = "Email is required"; } else{ $designation = input_validation($_POST["designation"]); } }
PHP Forms: Display Error Messages
Then we will add a small script after each required field in the HTML form, so that the script will be able to generate the appropriate error message if it is necessary (this is when the user tries to submit the form while not filling in all the required fields):
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> First name: <input type="text" name="firstname"><span class="error">* <?php echo $fnameErr; ?></span> <br><br> Last name: <input type="text" name="lastname"><span class="error">* <?php echo $lnameErr; ?></span> <br><br> Age: <input type="text" name="age"><span class="error">* <?php echo $ageErr; ?></span> <br><br> E-mail: <input type="text" name="email"><span class="error">* <?php echo $emailErr; ?></span> <br><br> <label for="yrofexp">Year of Experience:</label> <select name="yrofexp" id="yrofexp"> <option value="">Select number</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> </select> <br><br> Designation: <input type="text" name="designation"><span class="error">* <?php echo $designationErr; ?></span> <br><br> LinkedIn: <input type="text" name="linkedin"> <br><br> Write your Skills: <textarea name="skills" rows="5" cols="40"></textarea><span class="error">* <?php echo $skillsErr; ?></span> <br> <br><br> <input type="submit" name="submit" value="Submit"> </form>
Example: 
- Does the firstname, lastname, Designation, and skills fields contain only letters and whitespace?
- Is the Email field filled out with valid email address syntax?
- Is the age field filled out with a valid number greater than 0?