First, several element records used in the form validation
1.htmlspecialchars (), which is used to escape the special characters entered by the user as normal, such as < and > HTML characters are replaced with < and >
2.$_server["Php_self" is a super global variable that returns the current page script name
3.trim () used to remove extra spaces, etc.
4.stripslashes () to remove user-input backslash
Second simple form validation functions
function Test_input ($str) {
$str = trim ($STR);//Remove whitespace etc
$str = stripslashes ($STR);//remove backslash from user input
$str = Htmlspecialchars ($STR);//convert special characters to ordinary characters return
$str;
}
Prompt code for required items in form
1.php code, verify that the input box is empty, increase the error message for display in HTML, non-null validate and process input
2.HTML code inserts the output error message in the PHP code fragment such as <span class= "error" >*<?php Echo $EMAILERR?></span><br/>
$EMAILERR to save the error message variable, starting with null.
Simple example:
<! DOCTYPE html>
Show Chart:
Iv. Retention of form data
In the above code, just change the following <input> tags to add php fragment: <input type= ' name= ' value= ' <?php echo $name?> ' >
For <textarea> elements, simply add the PHP fragment between <textarea></textarea>
For radio, more trouble, add 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/> Comments: <textarea name=" Area "rows=" 4 "cols=" "><?php echo $area;? ></textarea><br/> Sex: <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" > Men <br/> <input type= "Submit" value= "submitted" name= "Submit" ><br/> </form>