日期:2014-05-16  浏览次数:20754 次

ThinkPHP学习笔记(十五)使用Ajax和Jquery来实现页面无刷新

Action

<?php
class MessageAction extends Action{
  	public function index(){
    	$this->display();
    }  
  	public function jquery(){
    	$this->display();
    }  
    public function add(){
    	$m=M('Message');
    	if ($vo=$m->create()){
    		if ($m->add()) {
//    			$this->success('添加成功');
				//使用ajax
    			$this->ajaxReturn($vo,'添加成功',1);
    		}else{
//    			$this->error('添加失败');
				//使用ajax
    			$this->ajaxReturn(0,'添加失败',0);
    		}
    	}else{
    		$this->error($m->getError());
    	}
    }
    public function addjquery(){
    	//ajaxReturn()
    	//返回json格式的数据
    	//参数一:数据;参数二:提示信息;三:状态
    	//echo $this->ajaxReturn(3,'提示信息',2);
    	$m=M('Message');
    	if ($m->add($_GET)){
    		$this->ajaxReturn($_GET,'添加信息成功',1);
    	}else{
    		$this->ajaxReturn(0,'添加信息失败',0);
    	}
    }
}
?>

jquery.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="__PUBLIC__/js/jquery-1.4.4.js"></script>
<script type="text/javascript">
//标准ie可用,firefox不可用
$(function(){
	$('input:button').click(function(){
			var $title=$('input[name="title"]').val();
			var $message=$('input[name="message"]').val();
			$msg=$('#msg');
			$.getJSON('__URL__/addjquery',{title:$title,message:$message},function(json){
				if(json.status==1){
					$msg.slideDown(2000,function(){
						$msg.css('display','block');
					}).html('标题为'+json.data.title+'信息为'+json.data.message);
				}else{
					$msg.slideDown(2000,function(){
						$msg.css('display','block');
					}).html('信息添加失败,请检查');
				}
		});
	})
})
</script>
<title></title>
</head>
<body>
<!-- 1.jquery实例,将jquery放入Public/js/之下 -->
<div style="display:none;color:red;" id="msg"></div>
<form action="__URL__/addjquery" method="get">
	标题:<input type="text" name="title"/><br/>
	信息:<input type="text" name="message"/><br/>
	<input type="button" value="提交"/><!-- 先设置为button不跳转 -->
</form>

</body>
</html>

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="__PUBLIC__/js/Base.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/prototype.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/mootools.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/ajax/ThinkAjax.js"></script>
<script type="text/javascript">
	function add(){
		ThinkAjax.sendForm('frm','__URL__/add',callback,'result');
	}
	function callback(data,status){
		alert(status);
		if(status==1){
			$('msg').innerHTML+='标题'+data.title+',信息'+data.message;
		}else{
			alert('参数错误');
		}
	}
</script>
<title></title>
</head>
<body>
<!-- 1.ajax实例,将js文件放入Public/js/之下 -->
<div style="color:red;" id="msg"></div>
<!-- 加入id,加入onclick事件 -->
<form action="__URL__/add" method="post" id="frm">
	标题:<input type="text" name="title"/><br/>
	信息:<input type="te