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" value="<?php echo $name;?>">
88 <span style="color:#FF0000"> * <?php echo $nameErr;?></span>
89 <p> 电邮 :
90 <input type="text" name="email" value="<?php echo $email;?>">
91 <span style="color:#FF0000"> * <?php echo $emailErr;?></span>
92 <p> 网址 :
93 <input type="text" name="website" value="<?php echo $website;?>">
94 <span style="color:#FF0000"> * <?php echo $websiteErr;?></span>
95 <p> 评论 :
96 <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
97 <p> 性别 :
98 <input type="radio" name = "gender"
99 <?php if(isset($gender)&&$gender=="female") echo "Checked";?>
100 value="female">女性
101 <input type="radio" name = "gender"
102 <?php if(isset($gender)&&$gender=="male") echo "Checked";?>
103 value="male">男性
104 <span style="color:#FF0000"> *</span>
105 <p>
106 <input type="submit" name="submit" value="提交">
107 </form>
108 <?php
109 echo "<h2>您的输入:</h2>";
110 echo $name;
111 echo "<br>";
112 echo $email;
113 echo "<br>";
114 echo $website;
115 echo "<br>";
116 echo $comment;
117 echo "<br>";
118 echo $gender;
119 ?>
120 <!--
121 (通过PHPtrim()函数)去除用户输入数据中不必要的字符(多余的空格、制表符、换行)
122 (通过 PHP stripslashes() 函数)删除用户输入数据中的反斜杠(\)
123 -->
124
125 </body>
126 </html>