Required and optional fields of forms for beginners to 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:
Display error message
In the following HTML example form, we provide a Several scripts have been added, each of which will display error messages when incorrect information is entered. (If the user submits the form without filling in the information, an error message will be output):
php中文网