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>