<!DOCTYPE HTML> <html> <head> <title></title> <meta charset="UTF-8"/> <style type="text/css"> .error{ color: #FF0000; } </style> </head> <body> <?php $name = $email = $gender = $comment = $website = $nameErr = $emailErr = $websiteErr = $textarea = $gender = ""; if($_SERVER["REQUEST_METHOD"] == "POST"){ if(empty($_POST[‘name‘])){ $nameErr = "姓名必须填写"; }else{ $name = test_input($_POST["name"]); } if(empty($_POST[‘email‘])){ $emailErr = "邮件必须填写"; }else{ $email = test_input($_POST["email"]); } $gender = test_input($_POST["gender"]); $comment = test_input($_POST["commet"]); $website = test_input($_POST["website"]); } function test_input($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>验证实例</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>"> 姓名:<input type="text" name="name"/><span class="error"><?php echo $nameErr; ?></span><br /> 邮件:<input type="email" name="email"/><span class="error"><?php echo $emailErr; ?></span><br /> 网址:<input type="text" name="website" /><span class="error"><?php echo $websiteErr; ?></span><br /> 评论:<textarea name="commet" cols="30" rows="10"></textarea><?php echo $textarea; ?><br /> 性别: <input type="radio" name="gender" value="female"/>女性 <input type="radio" name="gender" value="male" checked="checked"/>男性<br /><br /> <span class="error"><?php echo $gender; ?></span> <input type="submit" value=" 提交" /> </form> <?php echo "<h2>您的输入:</h2>"; echo $name; echo "<br />"; echo $email; echo "<br />"; echo $gender; echo "<br />"; echo $comment; echo "<br />"; echo $website; echo "<br />"; ?> </body> </html>
时间: 2024-10-06 22:58:10