PHP实现表单完整验证

拥有回忆 提交于 2019-12-06 11:32:24

一、表单实现样例

 

在表单中利用POST 提交信息,之后获取到后进行输出。

二、源码实现

在源码中有详细注释

  1 <!DOCTYPE html>
  2 <html>
  3 <head>
  4 <meta charset="UTF-8">
  5 <title>表单验证网页</title>
  6 <style>
  7 .error {
  8     color: #FF0000;
  9 }
 10 </style>
 11 </head>
 12 <body>
 13     <!-- php脚本对用户输入进行判断 -->
 14 <?php
 15 // 定义初始变量
 16 $nameErr = $emailErr = $genderErr = $websiteErr = ";";
 17 $name = $email = $gender = $website = $comment = $speciality = "";
 18 $hobbies = NULL;
 19 if ($_SERVER['REQUEST_METHOD'] == "POST") {
 20     // 对name输入进行验证
 21     if (empty($_POST["name"])) {
 22         $nameErr = "名字不能为空";
 23     } else {
 24         $name = test_input($_POST["name"]);
 25         if (! preg_match("/^[a-zA-Z ]*$/", $name)) {
 26             // 进行正则表达式的匹配,如果无法匹配,就设置$nameErr
 27             $nameErr = "只允许字母和空格";
 28         }
 29     }
 30     // 同理对email进行验证
 31     if (empty($_POST["email"])) {
 32         $emailErr = "邮箱不能为空";
 33     } else {
 34         $email = test_input($_POST["email"]);
 35         if (! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
 36             $emailErr = "邮箱格式不正确";
 37         }
 38     }
 39     // 个人网站
 40     if (empty($_POST["website"])) {
 41         $website = "";
 42     } else {
 43         $website = test_input($_POST["website"]);
 44         if (! preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $website)) {
 45             $websiteErr = "网站格式非法";
 46         }
 47     }
 48     // 简介部分
 49     if (empty($_POST["comment"])) {
 50         $comment = "";
 51     } else {
 52         $comment = test_input($_POST["comment"]);
 53     }
 54     // 性别
 55     if (empty($_POST["gender"])) {
 56         $genderErr = "性别是必需的";
 57     } else {
 58         $gender = test_input($_POST["gender"]);
 59     }
 60     // 专业
 61     if (empty($_POST["speciality"])) {
 62         $speciality = "";
 63     } else {
 64         $speciality = $_POST["speciality"];
 65     }
 66     // 爱好
 67     if (isset($_POST["hobbies"])) {
 68         $hobbies = $_POST["hobbies"];
 69     }
 70 }
 71 
 72 // 通过这个函数来对输入数据进行检测,防止CSS注入脚本攻击
 73 function test_input($data)
 74 {
 75     $data = trim($data); // 去除用户输入中的空格、tab、换行符等信息
 76     $data = stripcslashes($data); // 去除输入中的"/"反斜杠,防止有转义符的存在
 77     $data = htmlspecialchars($data); // 再次将数据转回html转义代码
 78     return $data;
 79 }
 80 ?>
 81 
 82 <h2>PHP 表单验证实例</h2>
 83     <p>
 84         <span class="error">* 必需字段。</span>
 85     </p>
 86 <!--     表单内容提交后均有记录,提交后不会清空当前页面的所有内容
 87         能够实现再次填写。
 88  -->
 89     <form method="post"
 90         action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
 91         名字: <input type="text" name="name" value="<?php echo $name;?>"> <span
 92             class="error">* <?php echo $nameErr;?></span> <br> <br> E-mail: <input
 93             type="text" name="email" value="<?php echo $email;?>"> <span
 94             class="error">* <?php echo $emailErr;?></span> <br> <br> 网址: <input
 95             type="text" name="website" value="<?php echo $website;?>"> <span
 96             class="error"><?php echo $websiteErr;?></span> <br> <br> 备注:
 97         <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
 98         <br> <br> 性别: <input type="radio" name="gender"
 99             <?php if (isset($gender) && $gender=="female") echo "checked";?>
100             value="female">女 <input type="radio" name="gender"
101             <?php if (isset($gender) && $gender=="male") echo "checked";?>
102             value="male">男 <span class="error">* <?php echo $genderErr;?></span><br>
103         专业: <select name="speciality" id="select">
104             <option value=""
105                 <?php if (isset($speciality) && $speciality=="") echo "selected";?>>请选择</option>
106             <option value="java"
107                 <?php if (isset($speciality) && $speciality=="java") echo "selected";?>>Java</option>
108             <option value="python"
109                 <?php if (isset($speciality) && $speciality=="python") echo "selected";?>>Python</option>
110             <option value="android"
111                 <?php if (isset($speciality) && $speciality=="android") echo "selected";?>>Android</option>
112             <option value="ios"
113                 <?php if (isset($speciality) && $speciality=="ios") echo "selected";?>>Ios</option>
114         </select><br> 爱好: <br> <input type="checkbox" name="hobbies[]"
115             value="game"
116             <?php if (isset($hobbies) && in_array("game", $hobbies)) echo "checked";?>>打游戏<br>
117         <input type="checkbox" name="hobbies[]" value="ball"
118             <?php if (isset($hobbies) && in_array("ball", $hobbies)) echo "checked";?>>打球
119         <br> <input type="checkbox" name="hobbies[]" value="music"
120             <?php if (isset($hobbies) && in_array("music", $hobbies)) echo "checked";?>>听音乐<br>
121 
122         <br> <br> <input type="submit" name="submit" value="Submit"
123             οnclick="selected()">
124     </form>
125     
126 <?php
127 echo "<h2>您输入的内容是:</h2>";
128 echo "姓名", $name;
129 echo "<br>";
130 echo "邮箱", $email;
131 echo "<br>";
132 echo "网站", $website;
133 echo "<br>";
134 echo "简介", $comment;
135 echo "<br>";
136 echo "性别", $gender;
137 echo "<br>";
138 echo "专业", $speciality;
139 echo "<br>";
140 echo "爱好";
141 if (is_array($hobbies)) {
142     for ($i = 0; $i < count($hobbies); $i ++) {
143         echo $hobbies[$i], ",";
144     }
145 }
146 ?>
147 </body>

 

转载于:https://www.cnblogs.com/null-/p/9717313.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!