ThinkPHP是一个快速、简单的基于MVC和面向对象的轻量级PHP开发框架.
这个星期主要学习了页面跳转、重定向,验证码、分页显示、上传文件。
**页面跳转和重定向训练目标
- 页面跳转和重定向
- 判断页面请求类型
- ajax请求和返回
- 系统的 Think\Controller 类内置了两个页面跳转方法error和success,分别用于错误(提示)跳转和成功(提示)跳转。两个方法都会输出一个提示信息页面,然后自动跳转到指定的地址。
- 如果不需要提示页面,ThinkPHP还可以实现直接重定向操作, Think\Controller 类提供了redirect方法,实现页面的重定向功能。
- 在很多情况下面,我们需要判断当前操作的请求类型是GET 、POST 、PUT或 DELETE,一方面可以针对 请求类型作出不同的逻辑处理,另外一方面有些情况下面需要验证安全性,过滤不安全的请求。系统内置了一些常量用于判断请求类型。
- ThinkPHP可以很好的支持AJAX请求,系统的 \Think\Controller 类提供了 ajaxReturn 方法用于AJAX调用后返回数据给客户端。并且支持 JSON 、 JSONP 、 XML 和 EVAL 四种方式给客户端接受数据,并且支持配置其他方式的数据格式返回。
- 实现学生登录功能。
- 模板
- <body>
- 登录页面
<form action="{:U('login')}" method="post" id="form1">
学号:<input type="text" name="no" id="no"><br>
密码:<input type="password" name="password" id="pwd"><br>
<input type="button" id="login" value="登录">
</form> - <script type="text/javascript" src="__PUBLIC__/jquery.min.js"></script>
- <script type="text/javascript">
$("#login").bind('click',function(event){
var url = $("#form1").attr('action');
$.post(url,$('#form1').serialize(),function(ret){
if(ret.error){
alert(ret.msg);
}else{
window.location.href=ret.url;
}
},'json')
});
</script> - </body>
- 控制器
- class IndexController extends Controller {
- public function login(){
- if(IS_GET){
$this->display();
}else if(IS_POST){
$where['no'] = I('no');
$password = I('password');
$where['password'] = md5($password);
$student = M('student')->field('no,name,sex,tel')->where($where)->find();
$url = U("Teacher/index");
if($student){
$this->ajaxReturn(array('error'=>false,'student'=>$student,'url'=>$url));
}else{
$this->ajaxReturn(array('error'=>true,'msg'=>'学号或密码错误'));
}
}
**验证码
class LoginController extends Controller{
//生成验证码
public function yzm(){
$config = array(
'length' => 4,//验证码位数
);
$Verify = new \Think\Verify($config);
$Verify->entry();
}
public function login(){
//get
if(IS_GET){
$this->display();
}else if(IS_AJAX){
$no=I('post.no');
$password=I('post.password');
$yzm = I('post.yzm');
if(empty($no) || empty($password)){
$this->ajaxReturn(array('ret'=>false,'msg'=>'用户名或密码不能为空'));
exit;
}else{
//验证输入的验证码是否正确
$Verify = new \Think\Verify();
if(!$Verify->check($yzm)){
$this->ajaxReturn(array('ret'=>false,'msg'=>'验证码输入不正确'));
exit;
}
}
$where['no']=$no;
$where['password']=md5($password);
$student=M('student')->field('no,name,sex,tel')->where($where)->find();
if($student){
$this->ajaxReturn(array('ret'=>true,'msg'=>'登录成功','url'=>U('teacher/index')));
}else{
$this->ajaxReturn(array('ret'=>false,'msg'=>'用户名或密码输入错误,登录失败'));
}
}
}
模板
<body>
登录页面
<form action="{:U('login')}" method="post" id="form1">
用户名:<input type="text" name="no" id="no"><br/>
密码:<input type="password" name="password" id="password"><br/>
验证码:<input type="text" name="yzm"><img src="{:U('yzm')}" id="yzm" width="200"></br/>
<input type="button" id="login" value="登录">
</form>
</body>
<script type="text/javascript" src="__PUBLIC__/jquery.min.js"></script>
<script type="text/javascript">
$('#yzm').bind('click',function(event){
$(this).attr('src',"{:U('yzm')}");
});
$('#login').bind('click',function(event){
var url=$('#form1').attr('action');
$.post(url, $('#form1').serialize(),function(data){
if(data.ret){
window.location.href=data.url;
}else{
alert(data.msg);
$('#yzm').trigger('click');
}
},'json');
});
</script>