首页 > 后端开发 > php教程 > php与ajax无刷新分页的实现代码(兼容多浏览器)

php与ajax无刷新分页的实现代码(兼容多浏览器)

WBOY
发布: 2016-07-25 09:00:16
原创
935 人浏览过
php与ajax结合实现的无刷新分页的简单例子,供初学的朋友参考。

无刷新分页,兼容IE与Firefox等浏览器。

1、index.php

<?php
 header("Content-Type:text/html;charset=utf-8");
?>
<html>
<head>
<title>无刷新分页_bbs.it-home.org</title>
<style>
A{text-decoration:none;}
A:link {COLOR:#33CCFF;}
A:active {COLOR:#FF6666;}    
A:visited {COLOR:#33CCFF;}    
A:hover {COLOR:#FF6699; TEXT-DECORATION: underline;position:relative;left:1px;top:1px}
</style>

<script language="javascript">
    $(document).ready(function(){
        changepage(1);
    });
    function changepage(page){
        $.post("sql.php",{page:page},function(data){
            $("#test").html(data);
        });
    }
</script>
</head>
<body>
    <ul id="test"></ul>
</body>
</html>
登录后复制

2、sql.php

<?php
mysql_connect("","root","");
mysql_select_db("test");
mysql_query("set names utf8");

if(isset($_POST["page"])){
    @$page = max(1, intval($_POST["page"]));
    $pagesize=10;
    $startindex=($page-1)*$pagesize;
    $sql="SELECT * FROM test ORDER BY id LIMIT $startindex,$pagesize";
    $rec=mysql_query($sql);
    while($row=mysql_fetch_array($rec)){
        $str.="<li>".$row["uname"]."</li>";
    }
    $num=mysql_num_rows(mysql_query("select * from test"));
    $pagenum=@ceil($num/$pagesize);
    for($i=1;$i<=$pagenum;$i++){
        if($page==$i){
            $str.="[".$i."]   ";
        }else{
            $str.="".$i."   ";
        }
    }
    echo $str;
}else{
    die();
}
?>
登录后复制

3、test数据库 test表

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(10) NOT NULL auto_increment,
  `uname` char(50) character set utf8 NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 COLLATE=gb2312_bin;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `test` VALUES ('1', 'aaaaaaaaaaa');
INSERT INTO `test` VALUES ('2', 'bbbbbbbbbbbb');
INSERT INTO `test` VALUES ('3', 'cccccccccccc');
INSERT INTO `test` VALUES ('4', 'dddddddddddddddd');
INSERT INTO `test` VALUES ('5', 'eeeeeeeeeee');
INSERT INTO `test` VALUES ('6', 'ffffffffff');
INSERT INTO `test` VALUES ('7', 'ggggggggggggggg');
INSERT INTO `test` VALUES ('8', 'hhhhhhhhhhhh');
INSERT INTO `test` VALUES ('9', 'jjjjjjjjjjjjjjjj');
INSERT INTO `test` VALUES ('10', 'kkkkkkkkkkk');
INSERT INTO `test` VALUES ('11', 'mmmmmmmmmmm');
INSERT INTO `test` VALUES ('12', 'cccccccccccccccccc');
INSERT INTO `test` VALUES ('13', 'bbbbbbbbbbbb');
INSERT INTO `test` VALUES ('14', 'ooooooooooooooooo');
INSERT INTO `test` VALUES ('15', 'pppppppppppp');
INSERT INTO `test` VALUES ('16', 'aaaaaaaaaaaaaaa');
INSERT INTO `test` VALUES ('17', 'ggggggggggggggggg');
INSERT INTO `test` VALUES ('18', 'dddddddddddddddd');
INSERT INTO `test` VALUES ('19', 'cccccccccccccc');
INSERT INTO `test` VALUES ('20', 'xxxxxxxxxx');
INSERT INTO `test` VALUES ('21', 'zzzzzzzzzzzzzzz');
INSERT INTO `test` VALUES ('22', 'oooooooooooooooooooo');
INSERT INTO `test` VALUES ('23', 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii');
INSERT INTO `test` VALUES ('24', 'uuuuuuuuuuuuuuuuuuuuuuuuuuuuu');
INSERT INTO `test` VALUES ('25', 'yyyyyyyyyyyyyyyyyy');
INSERT INTO `test` VALUES ('26', 'ttttttttttttttttttttttttttt');
INSERT INTO `test` VALUES ('27', 'rrrrrrrrrrrrrrrrrrrrrrrr');
INSERT INTO `test` VALUES ('28', 'eeeeeeeeeeeeeeeeeeeee');
INSERT INTO `test` VALUES ('29', 'wwwwwwwwwwwwwwwwwwwww');
INSERT INTO `test` VALUES ('30', 'qqqqqqqqqqqqq');
登录后复制


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