Required field will check whether the field is filled or not in the proper way. Most of cases we will use the * symbol for required field.
Validation means check the input submitted by the user. There are two types of validation are available in PHP. They are as follows −
The first thing we will do is to pass all variables through PHP's htmlspecialchars() function.
When we use the htmlspecialchars() function; then if a user tries to submit the following in a text field:
- this would not be executed, because it would be saved as HTML escaped code, like this:
<script>location.href('https://www.hacked.com')</script>
The code is now safe to be displayed on a page or inside an e-mail.
We will also do two more things when the user submits the form:
The next step is to create a function that will do all the checking for us (which is much more convenient than writing the same code over and over again).
We will name the function test_input().
Now, we can check each $_POST variable with the test_input() function, and the script looks like this: