-------------------------------------------------------------------------------------------
本节展示如何在用户提交表单后保留输入字段中的值。
-------------------------------------------------------------------------------------------
PHP - 保留表单中的值
如需在用户点击提交按钮后在输入字段中显示值,我们在以下输入字段的 value 属性中增加了一小段 PHP 脚本:name、email 以及 website。在 comment 文本框字段中,我们把脚本放到了 <textarea> 与 </textarea> 之间。这些脚本输出 $name、$email、$website 和 $comment 变量的值。
然后,我们还需要显示选中了哪个单选按钮。对此,我们必须操作 checked 属性(而非单选按钮的 value 属性):
1 Name: <input type="text" name="name" value="<?php echo $name;?>"> 2 3 E-mail: <input type="text" name="email" value="<?php echo $email;?>"> 4 5 Website: <input type="text" name="website" value="<?php echo $website;?>"> 6 7 Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> 8 9 Gender: 10 11 <input type="radio" name="gender" 12 <?php if (isset($gender) && $gender=="female") echo "checked";?> 13 value="female">Female 14 <input type="radio" name="gender" 15 <?php if (isset($gender) && $gender=="male") echo "checked";?> 16 value="male">Male
--------------------------------------------------------------------------------------------------------------
PHP - 完整的表单实例
下面是 PHP 表单验证实例的完整代码:
实例
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <style> 5 .error {color: #FF0000;} 6 </style> 7 </head> 8 <body> 9 10 <?php 11 // 定义变量并设置为空值 12 $nameErr = $emailErr = $genderErr = $websiteErr = ""; 13 $name = $email = $gender = $comment = $website = ""; 14 15 if ($_SERVER["REQUEST_METHOD"] == "POST") { 16 if (empty($_POST["name"])) { 17 $nameErr = "姓名是必填的"; 18 } else { 19 $name = test_input($_POST["name"]); 20 // 检查姓名是否包含字母和空白字符 21 if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 22 $nameErr = "只允许字母和空格"; 23 } 24 } 25 26 if (empty($_POST["email"])) { 27 $emailErr = "电邮是必填的"; 28 } else { 29 $email = test_input($_POST["email"]); 30 // 检查电子邮件地址语法是否有效 31 if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)) { 32 $emailErr = "无效的 email 格式"; 33 } 34 } 35 36 if (empty($_POST["website"])) { 37 $website = ""; 38 } else { 39 $website = test_input($_POST["website"]); 40 // 检查 URL 地址语法是否有效(正则表达式也允许 URL 中的斜杠) 41 if (!preg_match("/(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)) { 42 $websiteErr = "无效的 URL"; 43 } 44 } 45 46 if (empty($_POST["comment"])) { 47 $comment = ""; 48 } else { 49 $comment = test_input($_POST["comment"]); 50 } 51 52 if (empty($_POST["gender"])) { 53 $genderErr = "性别是必选的"; 54 } else { 55 $gender = test_input($_POST["gender"]); 56 } 57 } 58 59 function test_input($data) { 60 $data = trim($data); 61 $data = stripslashes($data); 62 $data = htmlspecialchars($data); 63 return $data; 64 } 65 ?> 66 67 <h2>PHP 验证实例</h2> 68 <p><span class="error">* 必需的字段</span></p> 69 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 70 姓名:<input type="text" name="name"> 71 <span class="error">* <?php echo $nameErr;?></span> 72 <br><br> 73 电邮:<input type="text" name="email"> 74 <span class="error">* <?php echo $emailErr;?></span> 75 <br><br> 76 网址:<input type="text" name="website"> 77 <span class="error"><?php echo $websiteErr;?></span> 78 <br><br> 79 评论:<textarea name="comment" rows="5" cols="40"></textarea> 80 <br><br> 81 性别: 82 <input type="radio" name="gender" value="female">女性 83 <input type="radio" name="gender" value="male">男性 84 <span class="error">* <?php echo $genderErr;?></span> 85 <br><br> 86 <input type="submit" name="submit" value="提交"> 87 </form> 88 89 <?php 90 echo "<h2>您的输入:</h2>"; 91 echo $name; 92 echo "<br>"; 93 echo $email; 94 echo "<br>"; 95 echo $website; 96 echo "<br>"; 97 echo $comment; 98 echo "<br>"; 99 echo $gender; 100 ?> 101 102 </body> 103 </html>
运行结果:http://www.w3school.com.cn/tiy/s.asp?f=demo_php_form_validation_escapechar