亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 145
粉絲 7
評(píng)論 7
訪問量 198520
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
PHP數(shù)據(jù)庫(kù)實(shí)操:無刷新分頁和編輯刪除功能
李東亞1??3????12?
原創(chuàng)
1263人瀏覽過

一、無刷新分頁

1.實(shí)現(xiàn)原理:前端通過ajax請(qǐng)求獲取服務(wù)器數(shù)據(jù),再由JS把數(shù)據(jù)組成html渲染到頁面中
2.服務(wù)器php代碼(獲取分頁數(shù)據(jù)信息)

  1. <?php
  2. require('connection.php');//鏈接數(shù)據(jù)庫(kù)操作
  3. //獲取當(dāng)前數(shù)據(jù)記錄總條數(shù)
  4. $sql="select count(*) as 'nums' from v_staffs;";
  5. $stmt=$pdo->prepare($sql);
  6. $stmt->execute();
  7. $result=$stmt->fetch();
  8. // var_dump($result);
  9. $nums=$result['nums'];
  10. $num=10;//每頁顯示記錄數(shù)
  11. $pages=ceil($nums/$num);//獲取總頁數(shù)
  12. // echo $pages;
  13. // 獲取當(dāng)前分頁的數(shù)據(jù)記錄信息
  14. $sql='select * from v_staffs limit ?,?;';
  15. // echo $_GET['p'];
  16. $page=$_GET['p']??1;//頁碼數(shù)
  17. $start=($page-1)*$num;//通過頁碼數(shù)獲取起始記錄數(shù)
  18. $stmt=$pdo->prepare($sql);
  19. $stmt->bindParam(1,$start,PDO::PARAM_INT);
  20. $stmt->bindParam(2,$num,PDO::PARAM_INT);
  21. $stmt->execute();
  22. // echo print_r($stmt->fetchAll(),true);
  23. $rows=$stmt->fetchAll();
  24. $res=['data'=>$rows,'pages'=>$pages];
  25. echo json_encode($res);
  26. // return json_encode($rows);

2.前端js獲取數(shù)據(jù)請(qǐng)求和頁面渲染

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>員工信息表(無刷新分頁)</title>
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  9. </head>
  10. <body>
  11. <main>
  12. <table border="1" cellpadding='3' cellspacing='0'>
  13. <caption>員工信息表</caption>
  14. <tr id="title">
  15. <th>編號(hào)</th>
  16. <th>姓名</th>
  17. <th>年齡</th>
  18. <th>性別</th>
  19. <th>工資</th>
  20. <th>郵箱</th>
  21. <th>職位</th>
  22. <th>負(fù)責(zé)區(qū)域</th>
  23. <th>操作</th>
  24. </tr>
  25. </table>
  26. <!--頁碼部分 -->
  27. <div id='page'>
  28. </div>
  29. </main>
  30. </body>
  31. <script>
  32. //頁面主體內(nèi)容獲取和生成函數(shù)
  33. function getrows(data) {
  34. //如果頁面已經(jīng)記錄信息需要移除
  35. if ($($("table>tbody")[0]).children().length > 1) {
  36. $($("table>tbody")[0]).children().remove("tr:not(#title)");
  37. };
  38. //生成html內(nèi)容
  39. var content = "";
  40. data.forEach((item) => {
  41. content += `<tr>
  42. <td>${item["id"]}</td>
  43. <td>${item["name"]}</td>
  44. <td>${item["age"]}</td>
  45. <td>${item["gender"]}</td>
  46. <td>${item["salary"]}</td>
  47. <td>${item["email"]}</td>
  48. <td>${item["postion"]}</td>
  49. <td>${item["area"]}</td>
  50. <td><button><a href="http://php.edu/test/edit.php?id=${item["id"]}" target="_blank">編輯</a></button><button class="delete" data-index="${item["id"]}">刪除</button></td>
  51. </tr>
  52. `;
  53. });
  54. //渲染到頁面中取
  55. $('#title').after(content);
  56. }
  57. //生成分條條函數(shù)
  58. function getpage(pages) {
  59. //如果沒有分頁條則生成
  60. if ($("#page").children().length == 0) {
  61. var plink = "<span>首頁</span><span>&lt</span>";
  62. for (var i = 1; i <= pages; i++) {
  63. plink += `<span>${i}</span>`;
  64. }
  65. plink += "<span>&gt;</span><span>尾頁</span>"
  66. // console.log(plink);
  67. $("#page").append(plink);
  68. }
  69. // console.log($("#page").children().length);
  70. }
  71. //ajax獲取數(shù)據(jù)信息函數(shù)
  72. function getdata(page) {
  73. $.ajax({
  74. type: 'GET',
  75. url: 'http://php.edu/test/staffs_api.php',
  76. data: {
  77. p: page
  78. },
  79. dataType: 'json',
  80. success: function(res) {
  81. // console.log(res);
  82. var data = res['data'];
  83. var pages = res['pages'];
  84. // console.log(data, pages);
  85. //調(diào)用函數(shù)生成頁面信息
  86. getrows(data);
  87. getpage(pages);
  88. }
  89. });
  90. }
  91. //首次打開頁面默認(rèn)獲取首頁數(shù)據(jù)信息并展示
  92. window.onload = function() {
  93. getdata(1);
  94. }
  95. //通過委托事件實(shí)現(xiàn)翻頁(上一頁下一頁功能沒有實(shí)現(xiàn))
  96. $('#page').click(function(ev) {
  97. // console.log(ev.target.textContent, ev.currentTarget);
  98. var currentpage = ev.target.textContent;
  99. switch (currentpage) {
  100. case "首頁":
  101. getdata(1);
  102. break;
  103. case "尾頁":
  104. var count = $(ev.currentTarget).children().length - 4;
  105. getdata(count);
  106. break;
  107. case "<":
  108. console.log('上一頁');
  109. break;
  110. case ">":
  111. console.log("下一頁");
  112. break;
  113. default:
  114. getdata(currentpage);
  115. }
  116. });
  117. //刪除數(shù)據(jù)功能,通過ajaxComplete()函數(shù)來獲取動(dòng)態(tài)生成的信息
  118. $(document).ajaxComplete(function() {
  119. // console.log($('.delete'));
  120. $('.delete').click(function(ev) {
  121. var id = $(ev.target).data("index");
  122. // console.log(id);
  123. if (window.confirm("確定刪除嗎?")) {
  124. $.ajax({
  125. type: 'GET',
  126. url: 'http://php.edu/test/delete.php',
  127. data: {
  128. id: id
  129. },
  130. dataType: 'json',
  131. success: function(res) {
  132. if (res[0] > 0) {
  133. return alert("刪除成功");
  134. } else {
  135. return alert("刪除失敗");
  136. }
  137. }
  138. });
  139. window.location.reload();
  140. } else {
  141. return false;
  142. }
  143. });
  144. });
  145. </script>
  146. <style>
  147. main {
  148. width: 1000px;
  149. margin: 10px auto;
  150. }
  151. table {
  152. width: 1000px;
  153. }
  154. #page {
  155. margin-top: 10px;
  156. width: 1000px;
  157. display: flex;
  158. justify-content: space-around;
  159. }
  160. #page>span {
  161. width: 46px;
  162. height: 20px;
  163. background-color: lightgray;
  164. /* border: 1px solid black; */
  165. text-align: center;
  166. color: whitesmoke;
  167. line-height: 20px;
  168. }
  169. #page>span:hover {
  170. cursor: pointer;
  171. background-color: white;
  172. color: red;
  173. }
  174. th,
  175. td {
  176. text-align: center;
  177. }
  178. </style>
  179. </html>

二、編輯功能

(一)功能實(shí)現(xiàn)原理
1.通過分頁的編輯按鈕獲取要編輯數(shù)據(jù)記錄的id,然后通過編輯頁面把要編輯的記錄顯示出來,
2.通過ajax獲取新的數(shù)據(jù),通過id,更新信息
(二)實(shí)現(xiàn)代碼
1.前端代碼

  1. <?php
  2. include('connection.php');
  3. $sql="select * from v_staffs where id=:id;";
  4. $stmt=$pdo->prepare($sql);
  5. $id=$_GET["id"];
  6. $stmt->bindParam(':id',$id,PDO::PARAM_INT);
  7. $stmt->execute();
  8. // $stmt->debugDumpParams();
  9. $row=$stmt->fetch();
  10. // var_dump($row);
  11. // exit();
  12. ?>
  13. <!DOCTYPE html>
  14. <html lang="en">
  15. <head>
  16. <meta charset="UTF-8">
  17. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  18. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  19. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  20. <title>員工信息修改</title>
  21. </head>
  22. <body>
  23. <body>
  24. <table border="1" cellpadding='3' cellspacing='0'>
  25. <caption>修改員工信息</caption>
  26. <tr>
  27. <th>編號(hào)</th>
  28. <th>姓名</th>
  29. <th>年齡</th>
  30. <th>性別</th>
  31. <th>工資</th>
  32. <th>郵箱</th>
  33. </tr>
  34. <tr>
  35. <td id="id"><?= $row["id"] ?></td>
  36. <td><input type="text" name="name" value="<?= $row["name"] ?>"></td>
  37. <td><input type="text" name="age" value="<?= $row["age"] ?>"></td>
  38. <td><input type="text" name="gender" value="<?= $row["gender"] ?>"></td>
  39. <td><input type="text" name="salary" value="<?= $row["salary"] ?>"></td>
  40. <td><input type="text" name="email" value="<?= $row["email"] ?>"></td>
  41. </tr>
  42. </table>
  43. <button id="update">提交</button>
  44. </body>
  45. <script>
  46. $("#update").click(function(){
  47. $.ajax({
  48. type: 'POST',
  49. url: 'http://php.edu/test/update.php',
  50. data: {
  51. //獲取新數(shù)據(jù)
  52. name: $("[name='name']").val(),
  53. age:$("[name='age']").val(),
  54. gender:$("[name='gender']").val(),
  55. salary:$("[name='salary']").val(),
  56. email:$("[name='email']").val(),
  57. id:$("#id").text()
  58. },
  59. dataType: 'json',
  60. success: function(res) {
  61. //確認(rèn)更新成功
  62. alert(res[1]);
  63. window.close();//關(guān)閉窗口
  64. }
  65. });
  66. });
  67. </script>
  68. </html>

2.后端代碼

  1. <?php
  2. include('connection.php');
  3. $sql="update staffs set name=?,age=?,gender=?,salary=?,email=? where id=?;";
  4. $stmt=$pdo->prepare($sql);
  5. $stmt->execute([$_POST['name'],$_POST["age"],$_POST["gender"],$_POST["salary"],$_POST["email"],$_POST['id']]);
  6. // $stmt->debugDumpParams();
  7. if($stmt->rowCount()>0){
  8. echo json_encode([$stmt->rowCount(),"更新成功"]);
  9. }else{
  10. echo json_encode([0,"更新失敗"]);
  11. }

運(yùn)行效果圖

批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)