ajax+php分页演示,带源码下载!!
今天看默默讲解分页,想想好像原创区很久没人发帖了,就顺便把默默的那个扩展开来,来个php+ajax分页演示吧,好的,说来就来,首先我们依然是基本的ajax开发框架:
复制内容到剪贴板
代码:
var http_request=false;
function send_request(url){//初始化,指定处理函数,发送请求的函数
http_request=false;
//开始初始化xmlhttprequest对象
if(window.xmlhttprequest){//mozilla浏览器
http_request=new xmlhttprequest();
if(http_request.overridemimetype){//设置mime类别
http_request.overridemimetype(text/xml);
}
}
else if(window.activexobject){//ie浏览器
try{
http_request=new activexobject(msxml2.xmlhttp);
}catch(e){
try{
http_request=new activexobject(microsoft.xmlhttp);
}catch(e){}
}
}
if(!http_request){//异常,创建对象实例失败
window.alert(创建xmlhttp对象失败!);
return false;
}
http_request.onreadystatechange=processrequest;
//确定发送请求方式,url,及是否同步执行下段代码
http_request.open(get,url,true);
http_request.send(null);
}
//处理返回信息的函数
function processrequest(){
if(http_request.readystate==4){//判断对象状态
if(http_request.status==200){//信息已成功返回,开始处理信息
document.getelementbyid(reobj).innerhtml=http_request.responsetext;
}
else{//页面不正常
alert(您所请求的页面不正常!);
}
}
}
function dopage(obj,url){
document.getelementbyid(obj).innerhtml=正在读取数据...;
send_request(url);
reobj=obj;
}
内容我放在一个div中显示,当翻页动作产生时,利用ajax更新div达到翻页效果这是内容显示页面代码:
复制内容到剪贴板
代码:
ajax分页演示
$pagenum){
echo error : can not found the page .$page;
exit;
}
$info=mysql_query(select * from cr_userinfo limit $offset,$num); //获取相应页数所需要显示的数据
while($it=mysql_fetch_array($info)){
echo $it['username'];
echo
;
} //显示数据
echo
;
echo $pagenav;//输出分页导航
?>
翻页的关键就在于翻页时调用dopage()函数,接着利用回调信息来更新div中的内容。服务器端核心代码:
复制内容到剪贴板
代码:
$pagenum){
echo error : can not found the page .$page;
exit;
}
$info=mysql_query(select * from cr_userinfo limit $offset,$num); //获取相应页数所需要显示的数据
while($it=mysql_fetch_array($info)){
echo $it['username'];
echo
;
} //显示数据
echo
;
echo $pagenav;//输出分页导航
?>