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.

QQ图片20161009120203.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 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):

"> 名字: *

E-mail: *

网址:

备注:

性别: *

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

.


Continuing Learning
||
"> 名字: *

E-mail: *

网址:

备注:

性别: *

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