php ajax无刷新分页,支持id定位
<?php
header("content-type:text/html; charset=utf-8");
function ajaxpage($total,$listnub,$currentpage,$url,$ajaxaction,$halfper='',$viewid='')
{
//计算总页数
$totalpage = @ceil($total/$listnub);
$total=$totalpage-1;
$re="";
//echo $currentpage;
$re .= ( $currentpage > 0 )
?
"<td><a
href=\"javascript:$ajaxaction('$url=0','$viewid')\"\">首页</a></td>\n<td><a
href=\"javascript:$ajaxaction('".$url."=".($currentpage-1)."','$viewid')\"\">上一页</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('$url=$i','$viewid')\">" . ( $i + 1 ) .
"</a></td>\n";
}
$re .= ( $currentpage < $total
)
? "<td><a
href=\"javascript:$ajaxaction('".$url."=".($currentpage+1)."','$viewid')\"\">下一页</a></td>\n<td><a
href=\"javascript:$ajaxaction('".$url."=".($total)."','$viewid')\"\">尾页</a>\n</td>"
:
"<td>下一页</td>\n<td>尾页</td>\n";
$re="<table
style=text-align:center><tr>$re</tr></table>";
return
$re;
}
//总页数,传递的页面变量-当前页 url地址 前后各多少页
$page = $_get['page'];
//echo
page('10',$page,'index.php?page','2');
?>
<div id="nike">
<?php
echo
ajaxpage(200,20,$page,'rand.php?page','ajaxaction','2','nike');
?>
</div>
<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('text/xml');
}
}
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('不能创建 xmlhttprequest 对象!');
return
false;
}
http_request.onreadystatechange = function
(){likeakak(htmlid);}//processrequest(htmlid)
http_request.open('get',
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('请求异常');
}
}
}
//处理返回信息
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('请求异常');
}
}
}
function
ajaxaction(url,viewid)
{
send_request(url,viewid);
}
//自定义调用函数
function elist(id)
{
var inputarray = new
array();
inputarray[1] = 'aaa';
inputarray[2] =
'bbb';
inputarray[3] = 'ccc';
inputarray[4] =
'ddd';
inputarray[5] = 'eee';
send_request('ajax.php?do=ajax&sort='+id,inputarray[id]);
}
</script>
上一篇: asp+ajax实现静态页面分页的代码
下一篇: 医疗笑话两个