A few elements recorded in form validation
1.htmlspecialchars (), which is used to escape the special characters entered by the user into normal character, such as HTML characters such as < and > are replaced with < and >
2.$_server["Php_self"] is a super global variable that returns the current page script name
3.trim () to remove extra spaces, etc.
4.stripslashes () to remove multiple input backslashes from the user
Second simple form validation functions
function Test_input ($str) {$str = Trim ($STR),//$str = Stripslashes ($STR),//Remove the backslash $str = Htmlspecialchars of the user input ($str );//convert special character to normal character return $str;}
Third, the form must fill in the prompt code
1.php code, verify that the input box is empty, add an error message for display in HTML, non-null to validate and process input
PHP code snippet for inserting output error information in 2.HTML code such as <span class= "error" >*<?php Echo $EMAILERR?></span><br/>
$EMAILERR to save the error information variable, the start is empty.
Simple example:
<! DOCTYPE html> To display a drawing:
Iv. Retention of form data
In the above code just change the following <input> tag to add PHP fragment: <input type= "name=" value= "<?php echo $name?>" >
For the <textarea> element, simply add the PHP fragment between <textarea> </textarea>
For radio, more hassle, 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/> review: <textarea name=" Area "rows=" 4 "cols=" "><?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= "submitted" name= "Submission" ><br/></form>
Basic PHP Tutorial-form validation (required, post-commit data retention)