首页 > 后端开发 > php教程 > php ajax无刷新分页,支持id定位示例代码

php ajax无刷新分页,支持id定位示例代码

怪我咯
发布: 2023-03-13 18:52:01
原创
1237 人浏览过

AJAX 是一种与服务器交换数据的技术,可以在不重新载入整个页面的情况下更新网页的一部分。本文章主要介绍php 使用ajax实现的无刷新分页代码

<?php 
Header("Content-Type:text/html; charset=utf-8"); 

function AjaxPage($Total,$ListNub,$CurrentPage,$Url,$AjaxAction,$HalfPer=&#39;&#39;,$ViewId=&#39;&#39;) 
{ 
//计算总页数 
$totalPage = @ceil($Total/$ListNub); 
$total=$totalPage-1; 
$re=""; 
//echo $CurrentPage; 
$re .= ( $CurrentPage > 0 ) 
?  
"<td><a  
href=\"javascript:$AjaxAction(&#39;$Url=0&#39;,&#39;$ViewId&#39;)\"\">首页</a></td>\n<td><a  
href=\"javascript:$AjaxAction(&#39;".$Url."=".($CurrentPage-1)."&#39;,&#39;$ViewId&#39;)\"\">上一页</a></td>\n" 
:  
"<td>首页</td>\n<td>上一页</td>\n"; 
for ( $i =  
$CurrentPage - $HalfPer,$i > 0 || $i = 0 ,     $j =  
$CurrentPage + $HalfPer, $j < $totalPage || $j = $totalPage;$i < $j ;$i++  
) 
{ 
  $re .= $i == $CurrentPage 
  ?  
"<td><b class=currentPage>[" . ( $i + 1 ) .  
"]</b></td>\n" 
  : "<td><a  
href=\"javascript:$AjaxAction(&#39;$Url=$i&#39;,&#39;$ViewId&#39;)\">" . ( $i + 1 ) .  
"</a></td>\n"; 
} 
$re .= ( $CurrentPage < $total  
) 
? "<td><a  
href=\"javascript:$AjaxAction(&#39;".$Url."=".($CurrentPage+1)."&#39;,&#39;$ViewId&#39;)\"\">下一页</a></td>\n<td><a  
href=\"javascript:$AjaxAction(&#39;".$Url."=".($total)."&#39;,&#39;$ViewId&#39;)\"\">尾页</a>\n</td>" 
:  
"<td>下一页</td>\n<td>尾页</td>\n"; 
$re="<table  
style=text-align:center><tr>$re</tr></table>"; 
return  
$re; 

} 
//总页数,传递的页面变量-当前页 url地址 前后各多少页 
$page = $_GET[&#39;page&#39;]; 
//echo  
page(&#39;10&#39;,$page,&#39;index.php?page&#39;,&#39;2&#39;); 


?> 

<p id="nike"> 
<?php 
echo  
AjaxPage(200,20,$page,&#39;rand.php?page&#39;,&#39;ajaxaction&#39;,&#39;2&#39;,&#39;nike&#39;); 
?> 
</p> 


<script type="text/javascript" language="javascript"> 
var  
http_request = false; 

function send_request(url,htmlid) { 
http_request =  
false; 
if (window.XMLHttpRequest) { 
  http_request = new  
XMLHttpRequest(); 
  if (http_request.overrideMimeType)  
{ 
   http_request.overrideMimeType(&#39;text/xml&#39;); 
  } 
}  
else if (window.ActiveXObject) { 
  try  
{ 
   http_request = new  
ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (e)  
{ 
   try { 
    http_request = new  
ActiveXObject("Microsoft.XMLHTTP"); 
   } catch (e)  
{} 
  } 
} 
if (!http_request)  
{ 
  alert(&#39;不能创建 XMLHttpRequest 对象!&#39;); 
  return  
false; 
} 
http_request.onreadystatechange = function  
(){likeakak(htmlid);}//processRequest(htmlid) 
http_request.open(&#39;GET&#39;,  
url, true); 
http_request.send(null); 

} 

//处理返回信息 
function processRequest(htmlid) { 
if  
(http_request.readyState == 1)  
{ 
  document.getElementById(htmlid).innerHTML="下载中..."; 
} 
if  
(http_request.readyState == 4) { 
  if (http_request.status == 200)  
{ 
   document.getElementById(htmlid).innerHTML=http_request.responseText; 
  }  
else  
{ 
   alert(&#39;请求异常&#39;); 
  } 
} 
} 
//处理返回信息 
function  
likeakak(htmlid) 
{ 
if (http_request.readyState == 1)  
{ 
  document.getElementById(htmlid).innerHTML="下载中..."; 
} 
if  
(http_request.readyState == 4) { 
  if (http_request.status == 200)  
{ 
   document.getElementById(htmlid).innerHTML=http_request.responseText; 
  }  
else  
{ 
   alert(&#39;请求异常&#39;); 
  } 
} 
} 
function  
ajaxaction(url,viewid) 
{ 
send_request(url,viewid); 
} 

//自定义调用函数 
function elist(id) 
{ 
var inputarray = new  
Array(); 

inputarray[1] = &#39;aaa&#39;; 
inputarray[2] =  
&#39;bbb&#39;; 
inputarray[3] = &#39;ccc&#39;; 
inputarray[4] =  
&#39;ddd&#39;; 
inputarray[5] = &#39;eee&#39;; 

send_request(&#39;ajax.php?do=ajax&sort=&#39;+id,inputarray[id]); 

} 

</script>
登录后复制

以上是php ajax无刷新分页,支持id定位示例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板