I don’t know if you have noticed that when we go online, some websites require us to register an account. When we register an account, we need to fill in the information. If we have important information that we have not filled in, we will be prompted. We tell you what is required to be filled in. In this chapter, we will explain the required fields and error messages of the form.
PHP - Required fields
Required fields are what we must fill in when we fill in the information, otherwise there is no way to pass,
In the previous chapter we have introduced the validation rules of the form. We can see that the "Name", "E-mail", and "Gender" fields are required. Each field cannot be empty. This is what is required. field.
##
# Name
is required. +Can only contain letters and spaces
E-mail
is required. + Must be a valid email address (contains '@' and '.')
## URL
Optional. If present, it must contain a valid URL
Comments
Optional. Multi-line input field (text field)
Gender
Required. Must select a
In the previous section, all input fields were optional because we did not validate them.
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 meaning of the above code is that if If we submit without writing anything in the three required fields of name, email, and gender, then the corresponding error message will be displayed on the page, which name is required and the email is required. If it is not blank, the verification will pass and the content in the else statement will be executed.
But the above code cannot display the error message. Let's look down.
#PHP - Display error message
In the following HTML example form, we have added some scripts for each field. Each script will display an error when the information is entered incorrectly. information. (If the user submits the form without filling in the information, an error message will be output):
In the above example, if we submit without filling in the content in the input boxes with *, the following page will be displayed:
Passed anyway. The next step is to validate the input data, i.e. "Does the Name field contain only letters and spaces?", and "Does the E-mail field contain valid email address syntax?", and if filled in Website field, "Does this field contain a valid URL?".
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning