页面一:
<form method="post" action="33-2.php">
Fullname:<input type='text' name='username' value=''/><br />
Password:<input type='password' name='password'/><br/>
C o d e s :<input type='text' name='code'/>1234<br />
EmailAd :<input type='text' name='email'/><br/>
Contents: <textarea rows=5 cols=25 name='content'></textarea><br />
<input type="submit" value='Submit it!' name='send'/><br/>
</form>
效果:
页面二:
<?php
//设置页面编码
header('Content-Type:text/html;charset=gbk');
//1.先验证是否是33-1.php提交过来的
//用isset()判断存在性,点击按钮提交的,就说明其他超级全局变量都存在;否则跳回到33-1.php的界面
if(!isset($_POST['send']) || $_POST['send']!='Submit it!'){ //如果不是点击按钮提交或者点击的不是submit is!按钮
header('location:33-1.php');
exit; //跳转到33-1.php页面,且不在执行下面的代码
}
//2.接收所有数据,并过滤
$username = trim($_POST['username']);
$password = $_POST['password'];
$code = trim($_POST['code']);
$email = trim($_POST['email']);
$content = htmlspecialchars(trim($_POST['content']));
//3.做判断
//用户名不能小于2位,不能大于10位
if(strlen($username)<2||strlen($username)>10){
// header('location:33-1.php'); //直接跳转不友好
echo "<script>alert('Username can not <2 or >10');history.back();</script>";
exit;
}else{
}
//密码不能小于6位
if(strlen($password)<6){
echo "<script>alert('the password can not <6');history.back();</script>";
exit;
}else{
}
//验证码必须是4位,且必须是数字
if(strlen(strlen($code)!=4||!is_numeric($code))){
echo "<script>alert('the code must be 4 and must be numeric');history.back();</script>";
exit;
}else{
}
//email格式,用正则表达式判断
if(!preg_match('/([\w\.]{2,255})@([\w\-]{1,255}).([a-z]{2,4})/',$email)){
echo "<script>alert('the mail address is not aviable!');history.back();</script>";
exit;
}else{
}
//4.输出所有信息
echo 'Username is:'.$username.'<br />';
echo 'Password is:'.$password.'<br />';
echo 'Code is:'.$code.'<br />';
echo 'Email is:'.$email.'<br />';
echo 'Content is:'.$content.'<br />';
?>
结果: