PHP entry-level tutorials-Details of forms and verification instances, and entry-level tutorials
This example describes PHP forms and verification. We will share this with you for your reference. The details are as follows:
Demo1.php
<? Php ob_start (); // redirect a URL // header () // header ('location: Demo2.php '); // The above sentence automatically jumps to the page you want. // Header ('location: http://www.baidu.com '); // The above sentence automatically jumps to Baidu above. Echo 'Baidu. com'; header ('location: http://www.baidu.com '); // before executing the header () function, you must note that there is no browser output before?>
Demo2.php
<? Php ob_start (); echo '000000'; // character encoding header ('content-Type: text/html; charset = gbk '); // set the Page code echo 'I am a Chinese';?>
Demo3.php
<Form method = "post" action = "Demo4.php"> Name: <input type = "text" name = "username"/> <br/> <input type = "submit" value = "submit"/> </form>
Demo4.php
<? Php // The first step is to receive the values in the previous form. // One, username // receives $ _ POST ['username'] // echo $ _ POST ['username']; // you need to understand the truth, A null string is also data. You can also assign a value to $ _ POST ['username']; // It is very accurate to use isset () to verify whether the request is submitted normally. // currently, the request is illegal, you did not submit the form and did not generate a global variable, instead of the username // field being empty // if (isset ($ _ POST ['username']). {// echo 'normal submit'; //} else {// echo 'illegal submit ';//}//! Empty ($ _ POST ['username']) and = ''are basically the same. However, it cannot be said that they are illegal. // It can only be said that they are not filled. // If (! Empty ($ _ POST ['username']) {// echo 'normal submit'; //} else {// echo 'illegal submit '; /// we recommend that you use if (isset ($ _ POST ['username']) {echo 'normal submit'; // before the output, for page Security, // echo $ _ POST ['username']; $ username =$ _ POST ['username']; $ username = trim ($ username ); $ username = htmlspecialchars ($ username); if (strlen ($ username) <2) {echo 'user name cannot be less than two places'; exit ();} if (! Is_numeric ($ username) {echo 'user name must be a pure number'; exit ();} echo $ username; // <strong> who asked Junyi? </strong>} else {echo 'illegal submit';}?>
Demo5.php
<Form method = "post" action = "Demo6.php"> username: <input type = "text" name = "username"/> <br/> password: <input type = "text" name = "password"/> <br/> Verification Code: <input type = "text" name = "code" size = "5"/> 1234 <br/> email box: <input type = "text" name = "email"/> <br/> introduction: <textarea rows = "6" cols = "25" name = "content"> </textarea> <br/> <input type = "submit" value = "submit" name = ""send"/> </form>
Demo6.php
<? Php // The first step is to verify whether it is submitted by Demo5.php. // as long as the button is clicked here, it indicates that, other super variable variables should exist // if send exists, click it. Otherwise, if (! Isset ($ _ POST ['send']) | $ _ POST ['send']! = 'Submit') {header ('location: Demo5.php '); exit; // skip back. If you do not need to execute the following, exit;} // step 2, receive all data $ username = trim ($ _ POST ['username']); $ password =$ _ POST ['Password']; $ code = trim ($ _ POST ['code']); $ email = trim ($ _ POST ['email ']); $ content = htmlspecialchars (trim ($ _ POST ['content']); // the user name cannot be less than 2 characters, and cannot be greater than 10 characters if (strlen ($ username) <2 | strlen ($ username)> 10) {echo "<script> alert ('user name cannot be less than two or greater than 10'); history. back (); </s Strong> "; exit;} // The password cannot be less than six if (strlen ($ password) <6) {echo "<script> alert ('password cannot be less than 6'); history. back (); </script> "; exit;} // The verification code must be 4 digits and must be a number if (strlen ($ code )! = 4 |! Is_numeric ($ code) {echo "<script> alert ('verification code must be 4 digits and a pure number'); history. back (); </script> "; exit;} // verify the email if (! Preg_match ('/^ ([\ w \.] {2,255}) @ ([\ w \-] {1,255 }). ([a-z] {2, 4}) $/', $ email) {echo "<script> alert ('invalid email address'); history. back (); </script> "; exit;} echo 'user name :'. $ username. '<br/>'; echo 'Email :'. $ email. '<br/>'; echo 'personal description :'. $ content;?>