gpt4 book ai didi

php+ajax无刷新分页实例详解

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章php+ajax无刷新分页实例详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了php+ajax无刷新分页实现方法。分享给大家供大家参考,具体如下:

ajax_page_show_userinfo.php页面如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<meta 'Content:text/html;charset=utf-8' ></meta>
<title>ajax分页演示</title>
<script language= "javascript" src= "js/ajaxpage.js" ></script>
<div id= "result" >
<?php
$db =mysql_connect( "localhost" , "root" , "123456" );
mysql_select_db( "register" );
mysql_query( "set names 'utf-8'" );
$result =mysql_query( "select * from user" );
$total =mysql_num_rows( $result ) or die (mysql_error());
$page =isset( $_GET [ 'page' ])? intval ( $_GET [ 'page' ]):1;
$page_size =5;
$url = 'ajax_page_show_userinfo.php' ;
$pagenum = ceil ( $total / $page_size );
$page =min( $pagenum , $page );
$prepage = $page -1;
$nextpage =( $page == $pagenum ?0: $page +1);
$pageset =( $page -1)* $page_size ;
$pagenav .= "显示第" .( $total ?( $pageset +1):0). "-" .min( $pageset +5, $total ). "记录&nbsp;共<b>" . $total . "</b>条记录&nbsp;现在是第&nbsp;<b>" . $page . "</b>&nbsp;页&nbsp;" ;
if ( $page <=1)
$pagenav .= "<a style=cursor:not-allowed;>首页</a>&nbsp;" ;
else
$pagenav .= "<a onclick=javascript:dopage('result','$url?page=1') style=cursor:pointer;>首页</a>&nbsp;" ;
if ( $prepage )
$pagenav .= "<a onclick=javascript:dopage('result','$url?page=$prepage') style=cursor:pointer;>上一页</a>&nbsp;" ;
else
$pagenav .= "<a style=cursor:not-allowed;>上一页</a>&nbsp;" ;
if ( $nextpage )
$pagenav .= "<a onclick=javascript:dopage('result','$url?page=$nextpage') style=cursor:pointer;>下一页</a>&nbsp;" ;
else
$pagenav .= "<a style=cursor:not-allowed;>下一页</a>&nbsp;" ;
if ( $pagenum )
$pagenav .= "<a onclick=javascript:dopage('result','$url?page=$pagenum') style=cursor:pointer;>尾页</a>&nbsp;" ;
else
$pagenav .= "<a style=cursor:not-allowed;>尾页</a>&nbsp;" ;
$pagenav .= "共" . $pagenum . "页" ;
if ( $page > $pagenum ){
   echo "error:没有此页" . $page ;
   exit ();
}
?>
<table align= "center" border= "2" width= "300" >
  <tr bgcolor= "#cccccc" align= "center" >
   <td>用户名</td>
   <td>用户密码</td>
  </tr>
<?php
$info =mysql_query( "select * from user limit $pageset,$page_size" );
while ( $array =mysql_fetch_array( $info )){
?>
  <tr align= "center" >
   <td><?php echo $array [ 'username' ];?></td>
   <td><?php echo $array [ 'password' ];?></td>
  </tr>
<?php 
}
?>
</table>
<?php
echo "<p align=center>$pagenav</p>" ;
?>
</div>

js文件下的ajaxpage.js:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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.getElementByIdx(reobj).innerHTML=http_request.responseText;
  }
  else { //页面不正常
   alert( "您所请求的页面不正常!" );
  }
   }
}
function dopage(obj,url){
   //document.getElementByIdx(obj).innerHTML="正在读取数据...";
   send_request(url);
   reobj=obj;
}

希望本文所述对大家PHP程序设计有所帮助.

最后此篇关于php+ajax无刷新分页实例详解的文章就讲到这里了,如果你想了解更多关于php+ajax无刷新分页实例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com