码迷,mamicode.com
首页 > Web开发 > 详细

PHP表单验证

时间:2015-07-15 13:10:58      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

  1 <!DOCTYPE html>
  2 <html>
  3 <head>
  4 <title>Test Code</title>
  5 </head>
  6 <body>
  7 <h1>
  8 My PHP Test
  9 <h1>
 10 <?php
 11     echo "<h1><span style=\"color:#ff0000;\">2015/7/15 上午</span></h1>";
 12     echo "A Simple Form-data<br>";
 13     #<a href="/demo/test_get.php?subject=PHP&web=W3school.com.cn">测试 $GET</a>
 14 ?>
 15 <!--一个简单的HTML表单-->
 16 <form action="<?php echo $_SERVER[‘PHP_SELF‘]?>"  method="post">
 17   Name:
 18   <input type="text" name="name">
 19   <br>
 20   E-mail:
 21   <input type="text" name="email">
 22   <br>
 23   <input type="submit">
 24 </form>
 25 <?php
 26     $name=$_REQUEST[‘name‘];
 27     echo $name;
 28     ?>
 29 <?php
 30     $nameErr = $emailErr = $genderErr = $websiteErr="";
 31     $name=$email=$gender=$comment=$website="";
 32     // 定义变量并设置为空值
 33 
 34     if ($_SERVER["REQUEST_METHOD"] == "POST") {
 35        if (empty($_POST["name"])) {
 36          $nameErr = "姓名是必填的";
 37        } else {
 38          $name = test_input($_POST["name"]);
 39          if(!preg_match("/^[a-zA-Z]*$/",$name)){
 40              $nameErr="只允许字母和空格";
 41             }
 42        }
 43        
 44        if (empty($_POST["email"])) {
 45          $emailErr = "电邮是必填的";
 46        } else {
 47          $email = test_input($_POST["email"]);
 48          //if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) 
 49          if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)){
 50              $emailErr="无效的 Email 格式 !";
 51          }
 52        }
 53          
 54        if (empty($_POST["website"])) {
 55          $website = "";
 56        } else {
 57          $website = test_input($_POST["website"]);
 58          if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
 59                $websiteErr = "无效的 URL !"; 
 60            }
 61        }
 62     
 63        if (empty($_POST["comment"])) {
 64          $comment = "";
 65        } else {
 66          $comment = test_input($_POST["comment"]);
 67        }
 68     
 69        if (empty($_POST["gender"])) {
 70          $genderErr = "性别是必选的";
 71        } else {
 72          $gender = test_input($_POST["gender"]);
 73        }
 74     }
 75     
 76     function test_input($data) {
 77        $data = trim($data);
 78        $data = stripslashes($data);
 79        $data = htmlspecialchars($data);
 80        return $data;
 81     }
 82 ?>
 83 <h2>表单验证实例</h2>
 84 <span style="color:#FF0000;">*必填字段</span>
 85 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP-SELF"]);?>">
 86   <p> 姓名 :
 87     <input type="text" name="name">
 88     <span style="color:#FF0000"> * <?php echo $nameErr;?></span>
 89   <p> 电邮 :
 90     <input type="text" name="email">
 91     <span style="color:#FF0000"> * <?php echo $emailErr;?></span>
 92   <p> 网址 :
 93     <input type="text" name="website">
 94     <span style="color:#FF0000"> * <?php echo $websiteErr;?></span>
 95   <p> 评论 :
 96     <textarea name="comment" rows="5" cols="40"></textarea>
 97   <p> 性别 :
 98     <input type="radio" name = "gender" value="female">
 99     女性
100     <input type="radio" name = "gender" value="male">
101     男性<span style="color:#FF0000"> *</span>
102   <p>
103     <input type="submit" name="submit" value="提交">
104 </form>
105 <?php
106     echo "<h2>您的输入:</h2>";
107     echo $name;
108     echo "<br>";
109     echo $email;
110     echo "<br>";
111     echo $website;
112     echo "<br>";
113     echo $comment;
114     echo "<br>";
115     echo $gender;
116     ?>
117 <!--    
118     (通过PHPtrim()函数)去除用户输入数据中不必要的字符(多余的空格、制表符、换行)
119     (通过 PHP stripslashes() 函数)删除用户输入数据中的反斜杠(\)
120     -->
121 
122 </body>
123 </html>

 

PHP表单验证

标签:

原文地址:http://www.cnblogs.com/zeze/p/php2.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!