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.


29.png


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):

"> 名字: *

E-mail: *

网址:

备注:

性别: *


Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!