1、demo1.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP表单验证</title>
</head>
<body>
<form action="check.php" method="post" target="_blank" >
<fieldset align="center">
<legend>注册</legend>
<p><label for="">用户 <input type="text" name="name"></label></p>
<p><label for="">邮箱 <input type="text" name="email"></label></p>
<p>性别:
<label><input type="radio" name="gender" value="male">帅哥</label>
<label><input type="radio" name="gender" value="female">美女</label>
<label><input type="radio" name="gender" value="shemale">人妖</label>
</p>
<p><label for="">年龄:
<select name="age" id="">
<option value="0">请选择年龄</option>
<option value="1">30以内</option>
<option value="2">30到60之间</option>
<option value="3">60以上</option>
</select>
</label>
</p>
<p>备注:<textarea name="comments" id="" cols="30" rows="10"></textarea></p>
</fieldset>
<p align="center"><input type="submit" value="提交"></p>
</form>
</body>
</html>
2、check.php
<?php
//姓名验证
$name=isset($_REQUEST['name']) ? $_REQUEST['name'] : null;
if (empty($name)){
echo '<script>alert("你没有输入姓名")</script>';
}else{
echo '<script>alert("你的用户名是:'.$name.'")</script>';
}
//邮箱验证
$email=isset($_POST['email']) ? $_POST['email'] : null;
if (empty($email)){
echo '<script>alert("你没有输入邮箱")</script>';
}else{
echo '<script>alert("你输入的邮箱是:'.$email.'")</script>';
}
//性别验证
$gender=isset($_REQUEST['gender']) ? $_REQUEST['gender'] : null;
switch ($gender){
case 'male':
echo '<script>alert("你是帅哥")</script>';
break;
case 'female':
echo '<script>alert("你是美女")</script>';
break;
case 'shemale':
echo '<script>alert("你是人妖")</script>';
break;
default :
echo '<script>alert("请输入性别")</script>';
break;
}
//年龄验证
$age=isset($_POST['age']) ? $_POST['age'] : null;
switch ($age){
case 0:
echo '<script>alert("你未选择年龄,请选择年龄")</script>';
break;
case 1:
echo '<script>alert("你的年龄在30以内")</script>';
break;
case 2:
echo '<script>alert("你的年龄在30到60之间")</script>';
break;
case 3:
echo '<script>alert("你的年龄在60以上")</script>';
break;
}