jquery ajax smarty,Ajax+smarty技术实现无刷新分页

这里运用Smarty模板,更简单

本文主要的技术:AJAX,PHP,Smarty,另外自己封装了一个很简单的类

类:

(function(){

function $(id) {

return document.getElementById(id);

}

$.init=function() {

try{return new XMLHttpRequest();}catch(e){};

try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e){}

alert('请更换浏览器');

}

$.get=function (url,data,callback,type) {

var xhr = this.init();

url += '?' +new Date().getTime();

if(data!=null){

url += '&'+data;

}

xhr.open('get',url);

xhr.onreadystatechange = function () {

if(xhr.readyState == 4 && xhr.status == 200){

if(type==null){

callback(xhr.responseText);

}

if(type == 'text'){

callback(xhr.responseText);

}

if(type == 'xml'){

callback(xhr.responseXML);

}

if(type == 'json'){

callback(eval("("+xhr.responseText+")"));

}

}

}

xhr.send(null);

}

$.post = function (url,data,callback,type) {

var xhr = this.init();

xhr.open('post',url);

xhr.setRequestHeader('Content-Type','Application/x-www-form-urlencoded');

xhr.onreadystatechange = function () {

if(xhr.readyState == 4 && xhr.status == 200){

if(type==null){

callback(xhr.responseText);

}

if(type == 'text'){

callback(xhr.responseText);

}

if(type == 'xml'){

callback(xhr.responseXML);

}

if(type == 'json'){

callback(eval("("+xhr.responseText+")"));

}

}

};

xhr.send(data);

}

类很简单,不介绍了

HTML:

window.οnlοad=function () {

init(2);

}

function del(id,p){

$.get('del.php','id='+id,function(msg){

if(msg==1){

init(p);

}

})

}

function init(p) {

$.get('page.php','page='+p,function(msg){

$('result').innerHTML=msg;

});

}

PHP:

header('content-type:text/html;charset=utf-8');

mysql_connect('localhost','root','root');

mysql_select_db('db2');

mysql_query('set names utf8');

$re=mysql_query('select count(*) as num from catgory');

$hang=mysql_fetch_assoc($re);

$row1=$hang['num'];

$page=isset($_GET['page'])? $_GET['page']:1;

$pagesize=2;

$total=ceil($row1/$pagesize);

$prev=$page-1;

$next=$page+1;

if($prev<1){$prev=1;}

if($next>$total){$next=$total;}

$offset = ($page-1)*$pagesize;

$result=mysql_query("select * from catgory limit ".$offset.','.$pagesize);

$ct=mysql_num_rows($result);

$data=array();

for($i=0;$i

$r=mysql_fetch_assoc($result);

$data[]=$r;

}

include('libs/Smarty.class.php');

$Smarty=new Smarty();

$Smarty->assign('data',$data);

$Smarty->assign('page',$page);

$Smarty->assign('pagesize',$pagesize);

$Smarty->assign('total',$total);

$Smarty->assign('prev',$prev);

$Smarty->assign('next',$next);

$Smarty->assign('row1',$row1);

$str=$Smarty->fetch('page.html');

echo $str;

Smarty:

编号品牌pid删除

{foreach from=$data item='value'}

{$value['id']}{$value['name']}{$value['pid']} 删除

{/foreach}

共{$total}页

第{$page}页

上一页

下一页

第一页

最末页

共{$row1}条数据

每页{$pagesize}条数据

要引入smary模板

以上所述是小编给大家介绍的Ajax+smarty技术实现无刷新分页,希望对大家有所帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值