php ajax 静态分页过程形式
(编辑:jimmy 日期: 2024/12/1 浏览:3 次 )
复制代码 代码如下:
js 端 及判断
<script language='javascript' >
$(function(){
//var url=$(this)[0].location.href;
//alert(url);
goPageFirst();
$('#selpage').bind('change',goPage);
});
//返回当前页数
function getNowPage()
{
return parseInt($('#nowPage').html());
}
//返回每页显示的条数
function getPer()
{
return $('#perPage').val();
}
//返回总记录数
function getTotal()
{
return parseInt($('#total').html());
}
//返回总页数
function getAllPage()
{
return parseInt($('#allpage').html());
}
//组合字符串参数
function getData(act)
{
var target=1;
var now=getNowPage()+1;
var per=getPer();
var sum=getTotal();
var allpage=getAllPage();
switch(act)
{
case 'first':
target=1;
break;
case 'prev':
target=now>1?now-1:1;
break;
case 'next':
target=now+1>allpage?now:1;
break;
case 'last':
target=allpage>0?allpage:1;
break;
case 'constans':
target=$('#selpage').val()>0?$('#selpage').val():1;
default :
target=1;
}
var data="check="+act+"&nowpage="+now+"&perpage="+per+'&target='+target;
alert(data);
return data;
}
//执行第一页
function goPageFirst()
{
var param=getData('first');
http_post(param);
}
//执行上一页
function goPagePrev()
{
var param=getData('prev');
http_post(param);
}
//执行下一页
function goPageNext()
{
var param=getData('next');
http_post(param);
}
//执行最后页
function goPageLast()
{
var param=getData('last');
http_post(param);
}
//选中到指定页面
function goPage()
{
var param=getData('constans');
http_post(param);
}
//post 请求数据加载
function http_post(db)
{
$.post('./mysql.php',db,function(data){
responseAjax(data);
},'text');
}
//返回数据的调处理
function responseAjax(msg)
{
//alert(msg);
//alert(msg.content);
//$('#listDiv').html(msg.content);
$('#listDiv').html(msg);
}
server端 判断及实现
//@autor 499188204@qq.com
mb_internal_encoding('gb2312');
$con=mysql_connect("127.0.0.1","root","12345678") or die("连接失败");
$m=mysql_select_db("ec",$con);
$sql = 'SELECT `attr_id`,`cat_id`,`attr_name`
FROM `ecs_attribute` ';
$rs = mysql_query($sql );
$count = mysql_num_rows($rs); //返回记录总数
//echo $count; //总记录数
//每页的记录数 10
//求总页数 ceil($count/10)
//echo '<hr >';
if(!isset($_POST['check']))
{
$set['id']=0;
die('no access please!');
}
if(isset($_POST['perpage']) && $_POST['perpage']>0 )
{
$per=$_POST['perpage'];
}
else
{
$per=15;
}
$page = ceil($count/$per);
//echo $page;
if(!isset($_POST['nowpage']))
{
$nowPage=1;
}
else
{
$nowPage = $_POST['nowpage'];
}
$start = ($nowPage-1)*$per+1;
$sql = 'select `attr_id`,`cat_id`,`attr_name` from `ecs_attribute` limit '.$start.','. $per;
//$sql = 'select `attr_id`,`cat_id`,`attr_name` from `ecs_attribute` limit 10 offset 20';
$rs = mysql_query($sql );
$str = "<table id='content'>
<tr><th>属性编号</th><th>类型编号</th><th>属性名字</th></tr>";
while($row = mysql_fetch_assoc($rs)){
$str .='<tr><td>'.$row['attr_id'].'</td><td>'.$row['cat_id'].'</td><td>'.$row['attr_name'].'</td><tr>';
}
$str.="</table>
<table id='page_list'>
<tr><td><div><span>总计<span id='total'>".$count."</span>个记录分为<span id='allpage'>".$page."</span>页当前第<span id='nowPage'>".$nowPage."</span>页,每页<input type='text' id='perPage' value='".$per."'/> |
<a href='javascript:void(0)' onclick='goPageFirst()'>第一页</a>
<a href='javascript:void(0)' onclick='goPagePrev()'>上一页</a>
<a href='javascript:void(0)' onclick='goPageFirst()'>下一页</a>
<a href='javascript:void(0)' onclick='goPageFirst()'>最末页 </a>
<select id='selpage' >";
for($i=1;$i <=$page;$i++)
{
if($nowPage==$i)
{
$str .= "<option selected >".$i."</option>";
}
else
{
$str .= "<option >".$i."</option>";
}
}
$str .="</select>
</span></div></td></tr>
</table>";
echo mb_convert_encoding($str,'UTF-8','gb2312');
//$set['content']=$str;
//echo json_encode($set);
?>
js 端 及判断
<script language='javascript' >
$(function(){
//var url=$(this)[0].location.href;
//alert(url);
goPageFirst();
$('#selpage').bind('change',goPage);
});
//返回当前页数
function getNowPage()
{
return parseInt($('#nowPage').html());
}
//返回每页显示的条数
function getPer()
{
return $('#perPage').val();
}
//返回总记录数
function getTotal()
{
return parseInt($('#total').html());
}
//返回总页数
function getAllPage()
{
return parseInt($('#allpage').html());
}
//组合字符串参数
function getData(act)
{
var target=1;
var now=getNowPage()+1;
var per=getPer();
var sum=getTotal();
var allpage=getAllPage();
switch(act)
{
case 'first':
target=1;
break;
case 'prev':
target=now>1?now-1:1;
break;
case 'next':
target=now+1>allpage?now:1;
break;
case 'last':
target=allpage>0?allpage:1;
break;
case 'constans':
target=$('#selpage').val()>0?$('#selpage').val():1;
default :
target=1;
}
var data="check="+act+"&nowpage="+now+"&perpage="+per+'&target='+target;
alert(data);
return data;
}
//执行第一页
function goPageFirst()
{
var param=getData('first');
http_post(param);
}
//执行上一页
function goPagePrev()
{
var param=getData('prev');
http_post(param);
}
//执行下一页
function goPageNext()
{
var param=getData('next');
http_post(param);
}
//执行最后页
function goPageLast()
{
var param=getData('last');
http_post(param);
}
//选中到指定页面
function goPage()
{
var param=getData('constans');
http_post(param);
}
//post 请求数据加载
function http_post(db)
{
$.post('./mysql.php',db,function(data){
responseAjax(data);
},'text');
}
//返回数据的调处理
function responseAjax(msg)
{
//alert(msg);
//alert(msg.content);
//$('#listDiv').html(msg.content);
$('#listDiv').html(msg);
}
server端 判断及实现
//@autor 499188204@qq.com
mb_internal_encoding('gb2312');
$con=mysql_connect("127.0.0.1","root","12345678") or die("连接失败");
$m=mysql_select_db("ec",$con);
$sql = 'SELECT `attr_id`,`cat_id`,`attr_name`
FROM `ecs_attribute` ';
$rs = mysql_query($sql );
$count = mysql_num_rows($rs); //返回记录总数
//echo $count; //总记录数
//每页的记录数 10
//求总页数 ceil($count/10)
//echo '<hr >';
if(!isset($_POST['check']))
{
$set['id']=0;
die('no access please!');
}
if(isset($_POST['perpage']) && $_POST['perpage']>0 )
{
$per=$_POST['perpage'];
}
else
{
$per=15;
}
$page = ceil($count/$per);
//echo $page;
if(!isset($_POST['nowpage']))
{
$nowPage=1;
}
else
{
$nowPage = $_POST['nowpage'];
}
$start = ($nowPage-1)*$per+1;
$sql = 'select `attr_id`,`cat_id`,`attr_name` from `ecs_attribute` limit '.$start.','. $per;
//$sql = 'select `attr_id`,`cat_id`,`attr_name` from `ecs_attribute` limit 10 offset 20';
$rs = mysql_query($sql );
$str = "<table id='content'>
<tr><th>属性编号</th><th>类型编号</th><th>属性名字</th></tr>";
while($row = mysql_fetch_assoc($rs)){
$str .='<tr><td>'.$row['attr_id'].'</td><td>'.$row['cat_id'].'</td><td>'.$row['attr_name'].'</td><tr>';
}
$str.="</table>
<table id='page_list'>
<tr><td><div><span>总计<span id='total'>".$count."</span>个记录分为<span id='allpage'>".$page."</span>页当前第<span id='nowPage'>".$nowPage."</span>页,每页<input type='text' id='perPage' value='".$per."'/> |
<a href='javascript:void(0)' onclick='goPageFirst()'>第一页</a>
<a href='javascript:void(0)' onclick='goPagePrev()'>上一页</a>
<a href='javascript:void(0)' onclick='goPageFirst()'>下一页</a>
<a href='javascript:void(0)' onclick='goPageFirst()'>最末页 </a>
<select id='selpage' >";
for($i=1;$i <=$page;$i++)
{
if($nowPage==$i)
{
$str .= "<option selected >".$i."</option>";
}
else
{
$str .= "<option >".$i."</option>";
}
}
$str .="</select>
</span></div></td></tr>
</table>";
echo mb_convert_encoding($str,'UTF-8','gb2312');
//$set['content']=$str;
//echo json_encode($set);
?>
下一篇:php中防止伪造跨站请求的小招式