PHP development basic tutorial form required fields
1. PHP required fields
In the previous chapter we have introduced the validation rules of the table, we can see "Name", "E -mail", and "gender" fields are required and cannot be empty.
If in the previous chapter, all input fields are optional.
In the following code we have added some new variables: $nameErr, $emailErr, $genderErr, and $websiteErr.. These error variables will be displayed on required fields. We also added an if else statement for each $_POST variable. These statements will check if the $_POST variable is empty (using PHP's empty() function). If it is empty, the corresponding error message will be displayed. If it is not empty, the data will be passed to the test_input() function:
The code is as follows:
2. Display error message
In the following HTML example form, we have added some scripts for each field. Each script will display an error message when the information is entered incorrectly. (If the user submits the form without filling in the information, an error message will be output):