首頁 >後端開發 >php教程 >php ajax無刷新分頁,支援id定位範例程式碼

php ajax無刷新分頁,支援id定位範例程式碼

怪我咯
怪我咯原創
2017-07-12 15:50:341220瀏覽

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn