Form required fields

巴扎黑
Release: 2016-11-12 09:26:59
Original
3347 people have browsed it

From the validation rules in the previous section, we see that the "Name", "E-mail" and "Gender" fields are required. These fields cannot be empty and must be filled in the HTML form.

Field Validation Rule

Name Required. Must contain letters and spaces.

E-mail required. Must contain a valid email address (including @ and .).

Website Optional. If optional, a valid URL must be included.

Comment Optional. Multi-line input field (text box).

Gender required. One must be selected.

In the previous section, all input fields were optional.

In the code below we have added some new variables: $nameErr, $emailErr, $genderErr and $websiteErr. These error variables hold the error message for the requested field. We also added an if else statement to each $_POST variable. This statement checks whether the $_POST variable is empty (via the PHP empty() function). If empty, the error message is stored in a different error variable. If not empty, the user input data is sent through the test_input() function:

Copy after login

PHP - Show error message

In the HTML form, we added a little script after each requested field. Appropriate error messages are generated if required (if the user attempts to submit the form without filling in required fields):

Example

"> Name: *

E-mail: *

Website:

Copy after login
    

PHP 验证实例

* 必需的字段

"> 姓名: *

电邮: *

网址:

评论:

性别: 女性 男性 *

您的输入:"; echo $name; echo "
"; echo $email; echo "
"; echo $website; echo "
"; echo $comment; echo "
"; echo $gender; ?>
Copy after login





PHP Validation Example


* Required fields


Name: *


Email: *


Website:


Comments:


Gender: Female Male



Your input:







Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!