Ajax+smarty技術(shù)實(shí)現(xiàn)無(wú)刷新分頁(yè)
這里運(yùn)用Smarty模板,更簡(jiǎn)單
本文主要的技術(shù):AJAX,PHP,Smarty,另外自己封裝了一個(gè)很簡(jiǎn)單的類
類:
(function(){ function $(id) { return document.getElementById(id); } $.init=function() { try{return new XMLHttpRequest();}catch(e){}; try{return new ActiveXObject('Microsoft.XMLHTTP');}catch(e){} alert('請(qǐng)更換瀏覽器'); } $.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); }
類很簡(jiǎn)單,不介紹了
HTML:
window.onload=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: <?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<$ct;$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:
<table border="1" rules="all" width="600"> <tr> <td>編號(hào)</td> <td>品牌</td> <td>pid</td> <td>刪除</td> </tr> {foreach from=$data item='value'} <tr> <td>{$value['id']}</td> <td>{$value['name']}</td> <td>{$value['pid']}</td> <td><a href="#", onclick="del({$value['id']},{$page})">刪除</td> </tr> {/foreach} <tr> <td colspan="3"> 共{$total}頁(yè) 第{$page}頁(yè) <a href="#", onclick="init({$prev})">上一頁(yè)</a> <a href="#", onclick="init({$next})">下一頁(yè)</a> <a href="#", onclick="init(1)">第一頁(yè)</a> <a href="#", onclick="init({$total})">最末頁(yè)</a> 共{$row1}條數(shù)據(jù) 每頁(yè){$pagesize}條數(shù)據(jù) </td> </tr> </table>
要引入smary模板
以上所述是小編給大家介紹的Ajax+smarty技術(shù)實(shí)現(xiàn)無(wú)刷新分頁(yè),希望對(duì)大家有所幫助!
相關(guān)文章
找到一款不錯(cuò)的基于AJAX留言板源碼(PHP版、ASP版)提供下載了
找到一款不錯(cuò)的基于AJAX留言板源碼(PHP版、ASP版)提供下載了...2007-09-09SpringMVC+Jquery實(shí)現(xiàn)Ajax功能
SpringMVC:是基于Spring的一個(gè)子框架(MVC框架),功能強(qiáng)于Spring,這個(gè)框架主要是解決咱們Controller這一層的問(wèn)題。這篇文章主要介紹了SpringMVC+Jquery實(shí)現(xiàn)Ajax功能,需要的朋友可以參考下2019-06-06PHP匹配連續(xù)的數(shù)字或字母的正則表達(dá)式
PHP匹配連續(xù)的數(shù)字或字母的正則表達(dá)式,需要的朋友可以參考下。2011-05-05Ajax 返回字符串的過(guò)濾實(shí)現(xiàn)代碼
在調(diào)用Ajax返回后。一個(gè)奇怪的問(wèn)題。返回的resultString值是“ok”但是跟字符串"ok"比較確不相等。2009-08-08django ajax提交評(píng)論并自動(dòng)刷新功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了django ajax提交評(píng)論并自動(dòng)刷新功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-06-06