Php basic tutorial-Form Verification (required, data retained after submission), after the basic tutorial is submitted

Source: Internet
Author: User

Php basic tutorial-Form Verification (required, data retained after submission), after the basic tutorial is submitted

I. Several element records used in Form Verification

1. htmlspecialchars () is used to escape special characters entered by the user into common characters. For example, HTML characters such as <and> are replaced with & lt; and & gt;

2. $ _ SERVER ["PHP_SELF"] is a super global variable. The script name on the current page is returned.

3. trim () is used to delete unnecessary spaces.

4. stripslashes () is used to delete multiple user-input backlash.

2. A simple form verification function

Function test_input ($ str) {$ str = trim ($ str); // remove spaces and other $ str = stripslashes ($ str ); // remove the backslash $ str = htmlspecialchars ($ str); // returns $ str ;}
3. Prompt code for required items in the form

1. in php code, if the verification input box is empty, an error message is added for display in HTML. If it is not empty, the input is verified and processed.

2. For example, <span class = "error"> * <? Php echo $ emailErr?> </Span> <br/>

$ EmailErr is the variable used to save the error message. The start value is null.

Simple Example:

<! Doctype html> 

Show cut chart:


Iv. Table Data Retention

In the above Code, you only need to add a PHP snippet to the <input> label as follows: <input type = ''name = ''value =" <? Php echo $ name?> ">

For <textarea> elements, you only need to add the php fragment between <textarea> </textarea>

It is difficult to select a single choice. Add the code in <input>: <? Php if (isset ($ gender) & $ gender = 'female') echo 'checked';?>

<Form method = "POST" action = "<? Php echo htmlspecialchars ($ _ SERVER ['php _ SELF ']);?> "> Name: <input type =" text "name =" name "value =" <? Php echo $ name;?> "> <Span class =" error "> * <? Php echo $ nameErr?> </Span> <br/> email: <input type = "text" name = "email" value = "<? Php echo $ email;?> "> <Span class =" error "> * <? Php echo $ emailErr?> </Span> <br/> URL: <input type = "text" name = "website" value = "<? Php echo $ website;?> "> <Br/> comment: <textarea name =" area "rows =" 4 "cols =" 40 "> <? Php echo $ area;?> </Textarea> <br/> gender: <input type = "radio" name = "gender" <? Php if (isset ($ gender) & $ gender = "female") echo "checked";?> Value = "female"> female <input type = "radio" name = "gender" <? Php if (isset ($ gender) & $ gender = "male") echo "checked";?> Value = "male"> male <br/> <input type = "submit" value = "submit" name = "submit"> <br/> </form>



Related Article

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.