源代码:
点击运行
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"><title>反馈表单验证 form validation required </title> </head> <body> <style>.error {color:#FF0000;} input[type=text], select, textarea { width:100%; padding:12px; border:1px solid #ccc; border-radius:4px; box-sizing:border-box; margin-top:6px; margin-bottom:16px; resize:vertical; } input[type=submit] { background-color:#4CAF50; color:white; padding:12px 20px; border:none; border-radius:4px; cursor:pointer; } input[type=submit]:hover { background-color:#45a049; } .container { border-radius:5px; background-color:#f2f2f2; padding:20px; } </style> <div class="container"> <?php // 定义变量并默认设置为空值 $contactpersonErr =$genderErr = $emailErr = $genderErr = $commentErr = $websiteErr = $countryErr = ""; $contactperson =$gender = $email = $gender = $comment = $website= $country = ""; if ($_SERVER["REQUEST_METHOD"] == "POST"){ if (empty($_POST["contactperson"])) { $nameErr = "名字是必需的"; }else{ $contactperson = test_input($_POST["contactperson"]); // 检测名字是否只包含字母跟空格 if (!preg_match("/^[a-zA-Z ]*$/",$contactperson)){ $contactpersonErr = "只允许字母和空格"; } } if (empty($_POST["email"])){ $emailErr = "邮箱是必需的"; }else{ $email = test_input($_POST["email"]); // 检测邮箱是否合法 if (!preg_match("/([w-]+@[w-]+.[w-]+)/",$email)){ $emailErr = "非法邮箱格式"; } } if (empty($_POST["website"])){ $website = ""; }else{ $website = test_input($_POST["website"]);// 检测 URL 地址是否合法 if (filter_var($website, FILTER_VALIDATE_URL) === FALSE) { $websiteErr = "非法的 URL 的地址"; } } if (empty($_POST["comment"])){ $comment = ""; }else{ $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])){ $genderErr = "性别是必需的"; }else{ $gender = test_input($_POST["gender"]); } ?> <?php echo "<h2>您输入的内容是:</h2> 姓名 ", $contactperson; echo "<br>性别 ",$gender; echo "<br>邮箱 ",$email; echo "<br>网址 ",$website; echo "<br>国家 ",$country,"<br>"; echo "<br>反馈信息 ",$comment,"<br>"; ?> </div> <?php } function test_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP 表单验证实例</h2> <div class="container"> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <label for="contactperson">Contact Person <?php echo $contactpersonErr;?></label> <input type="text" id="contactperson" name="contactperson" placeholder="请输入姓名.." value="马大哈"> <label for="email">EMAIL <?php echo $emailErr;?></label> <input type="text" id="email" name="email" placeholder="请输入邮箱.." value="madaha@qq.com"> <label for="website">website <?php echo $websiteErr;?></label> <input type="text" id="website" name="website" placeholder="请输入网址.." value="https://www.cctv.com"> <label for="country">COUNTRY <?php echo $countryErr;?></label> <select id="country" name="country"> <option value="australia">australia</option> <option value="canada">canada</option> <option value="usa">usa</option> </select> <label for="country">gender <?php echo $genderErr;?></label> <select id="gender" name="gender"> <option value="man">australia</option> <option value="woman">canada</option> </select> <label for="comment">反馈信息 <?php echo $commentErr;?></label> <textarea id="comment" name="comment" placeholder="反馈内容.." style="height:200px">哪有什么岁月静好 不过是有人替你负重前行</textarea> <input type="submit" value="提交"> </form> </div> </body> </html>
运行结果