您的位置:首页 > 编程语言 > PHP开发

php学习-表单验证2

2015-08-18 17:38 651 查看
<html>
<body>
<head>
<style>
.e{color:#ff0000}
</style>
</head>
<?php
$name = $email = $website = $comment = $sex = "";
$nameErr = $emailErr = $sexErr = "";
function input($data){
$data=trim($data);
$data=htmlspecialchars($data);
$data=stripslashes($data);
return $data;
}
$method=$_SERVER["REQUEST_METHOD"];
if($method=="POST"){
if(empty($_POST["name"])){
$nameErr="!姓名是必填项";
}else{
$name=input($_POST["name"]);
}

if(empty($_POST["email"])){
$emailErr="!邮箱不能为空";
}else{
$email=input($_POST["email"]);
}

if(empty($_POST["sex"])){
$sexErr="!性别为必选项";
}else{
$sex=input($_POST["sex"]);
}
$website=input($_POST["website"]);
$comment=input($_POST["comment"]);
}
?>

<h2>验证实例2</h2>
<p><span class="e">*必填字段</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
姓名:<input type="text" name="name">
<span class="e">*<?php echo $nameErr;?></span><br><br>
邮箱:<input type="text" name="email">
<span class="e">*<?php echo $emailErr;?></span><br><br>
网址:<input type="text" name="website"><br><br>
评论:<textarea name="comment" rows="5", cols="40"></textarea><br><br>
性别:<input type="radio" name="sex" value="male">男 <input type="radio" name="sex" value="female">女
<span class="e">*<?php echo $sexErr;?></span><br><br>
<input type="submit" name="submit" value="提交一下">
</form>

<?php
echo $name."<br>";
echo $email."<br>";
echo $website."<br>";
echo $comment."<br>";
echo $sex."<br>";
?>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: