PHP Basics Tutorial-form validation (required, data retention after submission)

Source: Internet
Author: User
Tags comments php basics

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 &lt; and &gt;

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> 



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.