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

怪我咯
Lepaskan: 2023-03-13 18:52:01
asal
1196 orang telah melayarinya

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>
Salin selepas log masuk

Atas ialah kandungan terperinci php ajax无刷新分页,支持id定位示例代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!