PHP表单验证内容是否为空的实现代码
作者:jingxian 时间:2024-05-13 09:51:21
内容为空效果图为:
填写内容效果图:
下面是验证程序的代码:
<!doctype html>
<html>
<head>
<meta http-equiv="conent-type" content="text/html" charset="utf-8"/>
<style>
.red{
color:red;
}
</style>
</head>
<body>
<?php
function test_input($data){
$data=trim($data);
$data=stripslashes($data);
$data=htmlspecialchars($data);
return $data;
}
?>
<?php
$name=$email=$web=$comment=$gender="";
$nameerr=$emailerr=$weberr=$commenterr=$gendererr="";
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']);
}
if(empty($_POST['web'])){
$weberr="必填网址";
}else{
$web=test_input($_POST['web']);
}
if(empty($_POST['comment'])){
$commenterr="必填备注";
}else{
$comment=test_input($_POST['comment']);
}
if(empty($_POST['gender'])){
$gendererr="必填备注";
}else{
$gender=test_input($_POST['gender']);
}
}
?>
<h1>表单验证</h1>
<span class="red">*必填字段</span>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
名字:<input type="text" name="name"/><span class="red"><?php echo "*".$nameerr;?></span>
<br/>
E-mail:<input type="text" name="email"/><span class="red"><?php echo "*".$emailerr;?></span>
<br/>
网址:<input type="text" name="web"/><span class="red"><?php echo "*".$weberr;?></span>
<br/>
备注:<textarea rows="10" cols="40" name="comment"></textarea><span class="red"><?php echo "*".$commenterr;?></span>
<br/>
性别:<input type="radio" name="gender" value="男"/>男<input type="radio" name="gender" value="女"/>女<span class="red"><?php echo "*".$gendererr;?></span>
<br/>
<input type="submit" value="提交验证"/>
</form>
<?php
echo "名字".$name;
echo "<br/>";
echo "E-mail:".$email;
echo "<br/>";
echo "网址:".$web;
echo "<br/>";
echo "备注:".$comment;
echo "<br/>";
echo "性别:".$gender;
echo "<br/>";
?>
</body>
</html>
标签:php,表单,验证
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python正则表达式匹配HTML页面编码
2023-07-29 06:50:36
使用SQL语句快速获取SQL Server数据字典
2009-01-08 16:31:00
刚学完怎么用Python实现定时任务,转头就跑去撩妹!
2022-07-09 06:50:02
PHP substr 截取字符串出现乱码问题解决方法[utf8与gb2312]
2024-04-30 09:57:37
ASP+FSO+框架实现ASP生成htm并分页的方法(批量)
2009-03-09 18:26:00
python类别数据数字化LabelEncoder VS OneHotEncoder区别
2023-10-12 07:46:46
![](https://img.aspxhome.com/file/2023/1/99571_0s.png)
WEB2.0网页制作标准教程(10)自适应高度
2008-02-19 19:21:00
详解Python中contextlib上下文管理模块的用法
2022-03-10 22:32:51
ORACLE 10g 安装教程[图文]
2009-05-24 19:12:00
![](https://img.aspxhome.com/file/UploadPic/20095/26/200952620153777.jpg)
js判断文件是否为utf-8编码的方法
2024-04-22 13:06:51
Python中re.findall()用法详解
2023-08-09 23:46:14
![](https://img.aspxhome.com/file/2023/1/61191_0s.png)
跟老齐学Python之不要红头文件(2)
2023-12-21 23:22:14
python 实现 pymysql 数据库操作方法
2024-01-22 09:56:29
uniapp引入模块化js文件和非模块化js文件的四种方式
2024-04-30 10:23:04
pytorch transform数据处理转c++问题
2023-08-19 11:24:49
Pytorch 高效使用GPU的操作
2021-10-25 04:45:23
![](https://img.aspxhome.com/file/2023/9/117549_0s.jpg)
python连接sql server数据库的方法实战
2024-01-22 03:28:38
![](https://img.aspxhome.com/file/2023/8/84948_0s.jpg)
gchart:基于google图表API的jquery组件全攻略:1、入门
2010-01-25 12:18:00
![](https://img.aspxhome.com/file/UploadPic/20101/25/chart001-34s.png)
asp HTTP_X_FORWARDED_FOR和REMOTE_ADDR
2010-07-02 12:33:00
Python Socket TCP双端聊天功能实现过程详解
2022-03-13 02:25:44