php ajax无刷新分页 支持id定位的代码分享

  1. <?php
  2. Header("Content-Type:text/html; charset=utf-8");
  3. function AjaxPage($Total,$ListNub,$CurrentPage,$Url,$AjaxAction,$HalfPer='',$View)
  4. {
  5. //计算总页数
  6. $totalPage = @ceil($Total/$ListNub);
  7. $total=$totalPage-1;
  8. $re="";
  9. //echo $CurrentPage;
  10. $re .= ( $CurrentPage > 0 )
  11. ?
  12. "<td><a
  13. href="javascript:$AjaxAction('$Url=0','$ViewId')"">首页</a></td>n<td><a
  14. href="javascript:$AjaxAction('".$Url."=".($CurrentPage-1)."','$ViewId')"">上一页</a></td>n"
  15. :
  16. "<td>首页</td>n<td>上一页</td>n";
  17. for ( $i =
  18. $CurrentPage - $HalfPer,$i > 0 || $i = 0 , $j =
  19. $CurrentPage + $HalfPer, $j < $totalPage || $j = $totalPage;$i < $j ;$i++
  20. )
  21. {
  22. $re .= $i == $CurrentPage
  23. ?
  24. "<td><b class=currentPage>[" . ( $i + 1 ) .
  25. "]</b></td>n"
  26. : "<td><a
  27. href="javascript:$AjaxAction('$Url=$i','$ViewId')">" . ( $i + 1 ) .
  28. "</a></td>n";
  29. }
  30. $re .= ( $CurrentPage < $total
  31. )
  32. ? "<td><a
  33. href="javascript:$AjaxAction('".$Url."=".($CurrentPage+1)."','$ViewId')"">下一页</a></td>n<td><a
  34. href="javascript:$AjaxAction('".$Url."=".($total)."','$ViewId')"">尾页</a>n</td>"
  35. :
  36. "<td>下一页</td>n<td>尾页</td>n";
  37. $re="<table
  38. ;
  39. return
  40. $re;
  41. }
  42. //总页数,传递的页面变量-当前页 url地址 前后各多少页
  43. $page = $_GET['page'];
  44. //echo
  45. page('10',$page,'index.php?page','2');
  46. ?>
  47. <div >
  48. <?php
  49. echo
  50. AjaxPage(200,20,$page,'rand.php?page','ajaxaction','2','nike');
  51. ?>
  52. </div>
  53. <script type="text/javascript" language="javascript">
  54. var
  55. http_request = false;
  56. function send_request(url,htmlid) {
  57. http_request =
  58. false;
  59. if (window.XMLHttpRequest) {
  60. http_request = new
  61. XMLHttpRequest();
  62. if (http_request.overrideMimeType)
  63. {
  64. http_request.overrideMimeType('text/xml');
  65. }
  66. }
  67. else if (window.ActiveXObject) {
  68. try
  69. {
  70. http_request = new
  71. ActiveXObject("Msxml2.XMLHTTP");
  72. } catch (e)
  73. {
  74. try {
  75. http_request = new
  76. ActiveXObject("Microsoft.XMLHTTP");
  77. } catch (e)
  78. {}
  79. }
  80. }
  81. if (!http_request)
  82. {
  83. alert('不能创建 XMLHttpRequest 对象!');
  84. return
  85. false;
  86. }
  87. http_request.onreadystatechange = function
  88. (){likeakak(htmlid);}//processRequest(htmlid)
  89. http_request.open('GET',
  90. url, true);
  91. http_request.send(null);
  92. }
  93. //处理返回信息
  94. function processRequest(htmlid) {
  95. if
  96. (http_request.readyState == 1)
  97. {
  98. document.getElementById(htmlid).innerHTML="下载中...";
  99. }
  100. if
  101. (http_request.readyState == 4) {
  102. if (http_request.status == 200)
  103. {
  104. document.getElementById(htmlid).innerHTML=http_request.responseText;
  105. }
  106. else
  107. {
  108. alert('请求异常');
  109. }
  110. }
  111. }
  112. //处理返回信息
  113. function
  114. likeakak(htmlid)
  115. {
  116. if (http_request.readyState == 1)
  117. {
  118. document.getElementById(htmlid).innerHTML="下载中...";
  119. }
  120. if
  121. (http_request.readyState == 4) {
  122. if (http_request.status == 200)
  123. {
  124. document.getElementById(htmlid).innerHTML=http_request.responseText;
  125. }
  126. else
  127. {
  128. alert('请求异常');
  129. }
  130. }
  131. }
  132. function
  133. ajaxaction(url,viewid)
  134. {
  135. send_request(url,viewid);
  136. }
  137. //自定义调用函数
  138. function elist(id)
  139. {
  140. var inputarray = new
  141. Array();
  142. inputarray[1] = 'aaa';
  143. inputarray[2] =
  144. 'bbb';
  145. inputarray[3] = 'ccc';
  146. inputarray[4] =
  147. 'ddd';
  148. inputarray[5] = 'eee';
  149. //phpfensi.com
  150. send_request('ajax.php?do=ajax&sort='+id,inputarray[id]);
  151. }
  152. </script>