PHP form - required fields

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.

## is required. +Can only contain letters and spaces 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)
# NameE-mail

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

Example

2.png



Continuing Learning

About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
Gender Required. Must select a 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):
    PHP中文网 

PHP 验证实例

* 必需的字段

"> 姓名: *

电邮: *

网址:

评论:

性别: 女性 男性 *

您的输入:"; echo $name; echo "
"; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>
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?".
||
PHP中文网

PHP 验证实例

* 必需的字段

"> 姓名: *

电邮: *

网址:

评论:

性别: 女性 男性 *

您的输入:"; echo $name; echo "
"; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>
submit Reset Code