PHP form-3 (PHP form-required field)

Source: Internet
Author: User
Tags php form

Phpform-Required fields

This section describes how to set required fields and error messages for a form.

PHP-Required Fields

In the previous section we have introduced the table validation rules, we can see "First name", "E-mail", and "gender" fields are required, each field cannot be empty.

Field validation rules
Name Necessary. + can contain only letters and spaces
E-Mail Necessary. + must contain a valid e-mail address (contains "@" and ".") )
Url Optional. If it exists, it must contain a valid URL
Note Optional. Multi-line field (text field).
Gender Necessary. One must be selected.

If in the previous section, all input fields are optional.

In the following code we have added some new variables: $NAMEERR, $EMAILERR, $GENDERERR, and $WEBSITEERR. These error variables are displayed on the 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 blank, the corresponding error message is displayed. If not NULL, the data is passed to the Test_input () function:

<?PHP//define a variable and set it to a null value by default$NAMEERR=$EMAILERR=$GENDERERR=$WEBSITEERR= "";$name=$email=$gender=$comment=$website= "";if($_server["request_method"] = = "POST") {  if(Empty($_post["Name"])) {    $NAMEERR= "Name is required." "; } Else {    $name= Test_input ($_post["Name"]); }  if(Empty($_post["Email"])) {    $EMAILERR= "Mailbox is required. "; } Else {    $email= Test_input ($_post["Email"]); }  if(Empty($_post["Website"])) {    $website= ""; } Else {    $website= Test_input ($_post["Website"]); }  if(Empty($_post["Comment"])) {    $comment= ""; } Else {    $comment= Test_input ($_post["Comment"]); }  if(Empty($_post["Gender"])) {    $GENDERERR= "Gender is required." "; } Else {    $gender= Test_input ($_post["Gender"]); }}?>
PHP-Displaying error messages

In the following HTML instance form, we have added some scripts for each field, and each script displays an error message when the information is entered incorrectly. (If the user submits the form without filling in the information, it will output an error message):

<form method= "POST" action= "<?php Echo htmlspecialchars ($_server[' php_self ']);? > ">name: <input type= "text" name= "name" > <spanclass= "Error" >* <?phpEcho $NAMEERR;? ></span> <br><br>E-Mail: <input type= "text" name= "email" > <spanclass= "Error" >* <?phpEcho $EMAILERR;? ></span> <br><br>website: <input type= "text" name= "website" > <spanclass= "Error" ><?phpEcho $WEBSITEERR;? ></span> <br><br>Notes: <textarea name= "comment" rows= "5" cols= "></textarea>" <br><br>Sex: <input type= "Radio" name= "Gender" value= "female" >female<input type= "Radio" name= "Gender" value= "male" >male<spanclass= "Error" >* <?phpEcho $GENDERERR;? ></span> <br><br> <input type= "Submit" name= "submit" value= "Submit" > </form>

PHP form-3 (PHP form-required field)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.