使用AJAX實現分頁

來源:互聯網
上載者:User

標籤:while   onclick   change   tar   start   郵箱   姓名   row   win   

Fenye.html

<!DOCTYPE html><html><head>    <title>分頁</title></head><script type="text/javascript">/*** AJAX* 1.建立ajax對象* 2.建立串連* 3.判斷ajax準備狀態及狀態代碼* 4.發送請求*/function showList(pageNow){    //建立ajax對象    var xmlHttp = new XMLHttpRequest();    //建立串連    xmlHttp.open(‘get‘,‘./AJAX_Fenye.php?pageNow=‘+pageNow);    //判斷ajax準備狀態及狀態代碼    xmlHttp.onreadystatechange = function(){        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {            //alert(xmlHttp.readyState);            document.getElementById(‘result‘).innerHTML = xmlHttp.responseText;        }    }    //發送請求    xmlHttp.send(null);}//預設顯示第1頁window.onload = function(){    showList(1);}</script><body><input type="text"><div id="result">    <!-- 此處顯示資訊! --></div></body></html>

 

Fenye.php

 

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>AJAX分頁</title></head><body><?php //以下php代碼可封裝調用,參數為pageNow//連結資料庫@mysql_connect(‘localhost‘,‘root‘,‘‘);@mysql_select_db(‘empmanage‘);@mysql_query(‘set name utf8‘);//擷取總記錄數$rs = mysql_query(‘select count(*) from emp‘);$rows = mysql_fetch_row($rs);$recordCount = $rows[0];//每頁顯示多少條pageSize$pageSize = 5;//總頁數 = 總記錄/每頁顯示多少$pageCount=ceil($recordCount/$pageSize);//擷取當前頁  三元運算 若不存在pageNow則預設顯示第1頁$pageNow = isset($_GET[‘pageNow‘])? $_GET[‘pageNow‘]:1;if ($pageNow < 1) {    $pageNow = 1;}elseif ($pageNow > $pageCount) {    $pageNow = $pageCount;}//起始位置  每頁從第幾條資料顯示$pageStart = ($pageNow-1)*$pageSize;//從哪條開始顯示,限制每頁顯示幾條$sql = "select * from emp limit $pageStart,$pageSize";//連結資料庫$rs = mysql_query($sql);//以上php代碼可封裝調用,參數為pageNow  返回查詢到的資料$rs ?> <table bordercolor="green" border="1">     <tr>         <th>ID</th>         <th>姓名</th>         <th>郵箱</th>         <th>等級</th>     </tr><?php  //迴圈取出資料while ($rows = mysql_fetch_assoc($rs)) { echo "        <tr>            <td>{$rows[‘id‘]}</td>            <td>{$rows[‘name‘]}</td>            <td>{$rows[‘email‘]}</td>            <td>{$rows[‘level‘]}</td>         </tr>    ";}?> <tr>     <td colspan=‘4‘>     <?php         //分頁頁碼  調用js中的showList()方法  此處$i=$pageNow             for ($i=1; $i <= $pageCount; $i++) {                  echo "<a href = ‘javascript:void(0)‘ onclick = ‘showList($i)‘>{$i}</a> &nbsp;";             }     ?>     </td> </tr> </table></body></html>

 

使用AJAX實現分頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.