PHP表单验证内容是否为空
内容为空效果图为:
填写内容效果图:
下面是验证程序的代码:
1 <!doctype html> 2 <html> 3 <head> 4 <meta http-equiv="conent-type" content="text/html" charset="utf-8"/> 5 <style> 6 .red{ 7 color:red; 8 } 9 </style> 10 </head> 11 <body> 12 <?php 13 function test_input($data){ 14 $data=trim($data); 15 $data=stripslashes($data); 16 $data=htmlspecialchars($data); 17 return $data; 18 } 19 ?> 20 <?php 21 $name=$email=$web=$comment=$gender=""; 22 $nameerr=$emailerr=$weberr=$commenterr=$gendererr=""; 23 if($_SERVER['REQUEST_METHOD']=="POST"){ 24 if(empty($_POST['name'])){ 25 $nameerr="必填名字"; 26 }else{ 27 $name=test_input($_POST['name']); 28 } 29 if(empty($_POST['email'])){ 30 $emailerr="必填邮件"; 31 }else{ 32 $email=test_input($_POST['email']); 33 } 34 if(empty($_POST['web'])){ 35 $weberr="必填网址"; 36 }else{ 37 $web=test_input($_POST['web']); 38 } 39 if(empty($_POST['comment'])){ 40 $commenterr="必填备注"; 41 }else{ 42 $comment=test_input($_POST['comment']); 43 } 44 if(empty($_POST['gender'])){ 45 $gendererr="必填备注"; 46 }else{ 47 $gender=test_input($_POST['gender']); 48 } 49 } 50 ?> 51 <h1>表单验证</h1> 52 <span class="red">*必填字段</span> 53 <form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"> 54 名字:<input type="text" name="name"/><span class="red"><?php echo "*".$nameerr;?></span> 55 <br/> 56 E-mail:<input type="text" name="email"/><span class="red"><?php echo "*".$emailerr;?></span> 57 <br/> 58 网址:<input type="text" name="web"/><span class="red"><?php echo "*".$weberr;?></span> 59 <br/> 60 备注:<textarea rows="10" cols="40" name="comment"></textarea><span class="red"><?php echo "*".$commenterr;?></span> 61 <br/> 62 性别:<input type="radio" name="gender" value="男"/>男<input type="radio" name="gender" value="女"/>女<span class="red"><?php echo "*".$gendererr;?></span> 63 <br/> 64 <input type="submit" value="提交验证"/> 65 </form> 66 <?php 67 echo "名字".$name; 68 echo "<br/>"; 69 echo "E-mail:".$email; 70 echo "<br/>"; 71 echo "网址:".$web; 72 echo "<br/>"; 73 echo "备注:".$comment; 74 echo "<br/>"; 75 echo "性别:".$gender; 76 echo "<br/>"; 77 ?> 78 </body> 79 </html>
作者:超级怪蜀黍
出处:http://www.cnblogs.com/UncleFreak/
作者新博客地址:https://www.z88j.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
邮箱:00@z88j.com,有什么问题和意见可以反馈给我!谢谢大家