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:

  1. First Name
  2. Last Name
  3. Age
  4. Email
  5. Designation
  6. Write your skills

It is mandatory fields and can’t be empty and submitted without being filled.

FieldsValidation Rules
First nameRequired field + There must only be letters and whitespace in the text
Last nameRequired field + There must only be letters and whitespace in the text
AgeRequired field + Valid number greater than 0
EmailRequired field + The email address must contain an (@ and .)
Year of ExperienceOptional field + If you have experience, enter a number more than 0
DesignationRequired field + There must only be letters and whitespace in the text
LinkedInOptional field + If have account on linkedIn then enter it
Write your SkillsRequired field + There must only be letters and whitespace in the text
All input fields were optional in the previous chapter.

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>
Merge Both HTML and PHP code:

Example: 

<!DOCTYPE HTML> <html lang="en"><head> <title>PHP Form</title> <style> .error { color: #FF0000; } </style> </head><body><?php $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 = "Skills is required"; } else { $skills = input_validation($_POST["skills"]); }if (empty($_POST["designation"])) { $designationErr = "Designation is required"; } else { $designation = input_validation($_POST["designation"]); }$linkedin = input_validation($_POST["linkedin"]); $yrofexp = input_validation($_POST["yrofexp"]); }// define variables and set to empty valuesfunction input_validation($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?><h2>PHP Form Validation</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> <p><span class="error">* required field</span></p> 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><?php echo "<h2>Your Form Data:</h2>"; echo "First Name: ".$firstname; echo "<br>"; echo "Last Name: ".$lastname; echo "<br>"; echo "Age: ".$age; echo "<br>"; echo "Email: ".$email; echo "<br>"; echo "Year of Experience: ".$yrofexp; echo "<br>"; echo "Designation: ".$designation; echo "<br>"; echo "LinkedIn URL: ".$linkedin; echo "<br>"; echo "Skills: ".$skills;?></body></html>
After input data is validated, the next step is to check to see if:
  • 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?
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *