Form required fields help to present an error message when the user tries to submit the form without filling the required details. The php script will check the user’s field when form submitted and if any of the field data is missing it will show the error message in the form. It will show on the form which informs users that certain fields are mandatory so he has to fill those details.
Php require fields validation can be achieved by adding some extra validation code to the php form.
Example
[php]<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>[/php]
Code Explanation:
In the above code first, we define the variable with the empty value in the php code.
[php]$nameErr = $emailErr = $genderErr = $websiteErr = "";[/php]
[php]$name = $email = $gender = $comment = $website = "";[/php]
In the next step will create the if statement to confirm the form validation through method. If the form is a field with genuine post method then it will run the rest of the code.
if ($_SERVER["REQUEST_METHOD"] == "POST")
Now we want to validate each of the form fields by giving extra validation authority. To do that first we will validate whether the form field is empty or not. For that, we will be creating another if statement inside the main if statement to run the code.
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
In the above code, we are asking php script to check if statement where we have used a function called empty(). The empty function checks whether the form field is filled or not. If it is empty then it will process the first statement which is $nameErr ="Name is required" and if the field has data then it will send to the echo statement to record the field by using POST statement. The above code is validating the data for the field name. Similarly, you have to use the validation code for all the form fields to validate each field.
After that, we will be validating the data by using php security validator functions.
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
To present the error message in the HTML form you need to add php echo statement. Change the error message variable to their respective form fields.