PHP form required fields
PHP Form - Required Fields
In this chapter we will introduce how to set the required fields and error messages of the form.
PHP - Required fields
In the previous chapter we have introduced the validation rules of the table, we can See that the "Name", "E-mail", and "Gender" fields are required and each field 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 not empty, the data will be passed to the test_input() function:
##PHP - 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): Note: The so-called required fields are not static, and some are determined according to needs, such as some People just don’t have E-mail, or filling in or not filling in E-mail has no obvious impact on the use effect, so you can leave it blank or choose to fill it in. This depends on the actual situation .