PHP Introductory tutorials for forms and validation examples _php tips

Source: Internet
Author: User

This example describes the PHP form and validation. Share to everyone for your reference, specific as follows:

demo1.php

<?php
  Ob_start ();
  REDIRECT a URL
  //header ()
  //header (' Location:Demo2.php ');
  The above sentence can automatically jump to the page you want.
  //header (' location:http://www.baidu.com ');
  Above this sentence automatically jump to Baidu above.
  echo ' baidu.com ';
  Header (' location:http://www.baidu.com ');
  In executing the header () function, it must be noted that there is no browser output before
?>

demo2.php

<?php
  Ob_start ();
  echo ' 1232 ';
  Character encoding
  header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK ');//Set page encoding
  Echo ' I am Chinese ';
? >

demo3.php

<form method= "POST" action= "demo4.php" >
  name: <input type= "text" name= "username"
  /><br/> <input type= "Submit" value= "submitted"/>
</form>

demo4.php

 <?php//First step, receive the values in the previous form.
  One, username//Receive $_post[' username ']//echo $_post[' username '];
  You need to understand that an empty string is also data and can be assigned to $_post[' username ';   Using Isset () to verify that the normal submission is very accurate//currently referred to as illegal submission, is that you did not submit the form, no global variables, and not the username///if (Isset ($_post[' username '])) {//
Echo ' normal submission ';
}else{//Echo ' illegally submitted ';//}//!empty ($_post[' username ') and = = ' basically the same, but, can not say, people are illegal//can only say that they did not fill it. if (!empty ($_post[' username '))) {//echo ' normal submission ';//}else{//echo ' illegal submission ';/////suggest using if (Isset ' $_post[
    ']) {echo ' normal submission ';
    Before the output, for page security//echo $_post[' username '];
    $username = $_post[' username '];
    $username = Trim ($username);
    $username = Htmlspecialchars ($username);
      if (strlen ($username) < 2) {echo ' username cannot be less than two bits ';
    Exit ();
      } if (!is_numeric ($username)) {echo ' username must be a pure number ';
    Exit (); echo $username;
  <strong> read who asked the chant </strong>}else{echo ' illegal submission '; }?> 

demo5.php

<form method= "POST" action= "demo6.php" >
  username: <input type= "text" name= "username"/><br/>
  Secret Code: <input type= "text" name= "password"/><br/>
  Authentication code: <input type= "text" name= "code" size= "5"/>1234 <br/>
  mailbox: <input type= "text" name= "email"/><br/>
  Introduction: <textarea rows= "6" cols= "Name=" Content "></textarea><br/>
  <input type=" Submit "value=" submitted "name=" send "/>
</form >

demo6.php

<?php//First step, first verify whether the demo5.php submitted over//As long as the button point to me here, then it means that other super variables should exist//if Send is present, then say it is point, otherwise, skip back if (!isset $ _post[' send ']) | |
    $_post[' send ']!= ' submit ') {header (' Location:Demo5.php '); Exit
  Jump back, there is no need to execute, then exit;//Step two, receive all data $username = Trim ($_post[' username '));
  $password = $_post[' password '];
  $code = Trim ($_post[' code ']);
  $email = Trim ($_post[' email ')];
  $content = Htmlspecialchars (Trim ($_post[' content ')); User name cannot be less than 2 bits, not greater than 10-bit if (strlen ($username) <2| |
    Strlen ($username) >10) {echo <script>alert (' username cannot be less than two digits or greater than ten '); History.back ();</script> ";
  Exit
    }//password cannot be less than six bit if (strlen ($password) <6) {echo <script>alert (' password cannot be less than 6 bits '); History.back ();</script> ";
  Exit The//Verification code must be 4 digits, must be a number if (strlen ($code)!=4 | |!is_numeric ($code)) {echo <script>alert (' captcha must be 4 digits and is a pure number '); his
    Tory.back ();</script> ";
  Exit //Verify e-mail if (!preg_match ('/^) ([\w\.] {2,255}) @ ([\w\-]{1,255}). ([a-z]{2,4})$/', $email)) {echo "<script>alert (' e-mail is illegal '); History.back ();</script>";
  Exit Echo ' username: '. $username. '
  <br/> '; Echo ' e-mail: '. $email. '
  <br/> ';

 Echo ' Personal introduction: '. $content;?>

More interested in PHP related content readers can view the site topics: "PHP array" Operation Tips Encyclopedia, "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with the PHP program design.

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.