All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Empty Field Validation In PHP

Last Updated : Mar 11, 2024

Empty Field Validation In PHP

In this article we will show you the solution of empty field validation in PHP, the "Name", "E-mail", & "Gender" fields are needed, as can be seen from the validation criteria table on the preceding page.

These fields in the HTML form have to be filled out and cannot be left empty. The new variables $nameErr,$emailErr,$genderErr, & $websiteErr have been introduced to the code below.

These error variable will store any notifications related to the mandatory fields. For each $_POST variable, an if else clause has also been added.

We will now discuss the idea of how to create empty field validation using php with an example.

Step By Step Guide On Empty Field Validation In PHP :-

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail:
<input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website:
<input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
  1. The form is opened with method "post" and action set to the current PHP file.
  2. The first input field is for the user's name with the "name" attribute set to "name". This will be used later in PHP code to retrieve the value entered by the user.
  3. A span element with class "error" is used to display any error message related to the user's name input. The PHP variable $nameErr will be used to display an error message if there is an issue with the user's name input.
  4. A line break (br) tag is added for formatting.
  5. The second input field is for the user's email with the "name" attribute set to "email".
  6. A span element with class "error" is used to display any error message related to the user's email input. The PHP variable $emailErr will be used to display an error message if there is an issue with the user's email input.
  7. Another line break (br) tag is added for formatting.
  8. The third input field is for the user's website with the "name" attribute set to "website". This field is optional, so there is no error message associated with it.
  9. Another line break (br) tag is added for formatting.
  10. A textarea element is added for the user's comment. The "name" attribute is set to "comment", and the size is set to 5 rows and 40 columns.
  11. Another line break (br) tag is added for formatting.
  12. Three radio buttons are added for the user's gender selection. The "name" attribute is set to "gender", and each button has a different value ("female", "male", or "other").
  13. A span element with class "error" is used to display any error message related to the user's gender selection. The PHP variable $genderErr will be used to display an error message if there is an issue with the user's gender selection.
  14. Another line break (br) tag is added for formatting.
  15. A submit button is added with the "name" attribute set to "submit" and the value set to "Submit".
  16. The form is closed with the /form tag.

Conclusion :-

As a result, we have successfully learned how to create empty field validation using php with an example.

In conclusion, PHP form validation was a crucial step in the creation of websites since it helps to ensure the security and correctness of user-submitted data.

PHP makes it simple to install form validation on your own website thanks to the diversity of built-in methods and the capability to handle failures properly.

This book gives you the knowledge you need to begin using PHP form validation, whether you are a novice or an experienced developer.

I hope this article on empty field validation in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪