
批改狀態(tài):合格
老師批語:
1.實(shí)現(xiàn)原理:前端通過ajax請(qǐng)求獲取服務(wù)器數(shù)據(jù),再由JS把數(shù)據(jù)組成html渲染到頁面中
2.服務(wù)器php代碼(獲取分頁數(shù)據(jù)信息)
<?php
require('connection.php');//鏈接數(shù)據(jù)庫(kù)操作
//獲取當(dāng)前數(shù)據(jù)記錄總條數(shù)
$sql="select count(*) as 'nums' from v_staffs;";
$stmt=$pdo->prepare($sql);
$stmt->execute();
$result=$stmt->fetch();
// var_dump($result);
$nums=$result['nums'];
$num=10;//每頁顯示記錄數(shù)
$pages=ceil($nums/$num);//獲取總頁數(shù)
// echo $pages;
// 獲取當(dāng)前分頁的數(shù)據(jù)記錄信息
$sql='select * from v_staffs limit ?,?;';
// echo $_GET['p'];
$page=$_GET['p']??1;//頁碼數(shù)
$start=($page-1)*$num;//通過頁碼數(shù)獲取起始記錄數(shù)
$stmt=$pdo->prepare($sql);
$stmt->bindParam(1,$start,PDO::PARAM_INT);
$stmt->bindParam(2,$num,PDO::PARAM_INT);
$stmt->execute();
// echo print_r($stmt->fetchAll(),true);
$rows=$stmt->fetchAll();
$res=['data'=>$rows,'pages'=>$pages];
echo json_encode($res);
// return json_encode($rows);
2.前端js獲取數(shù)據(jù)請(qǐng)求和頁面渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>員工信息表(無刷新分頁)</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<main>
<table border="1" cellpadding='3' cellspacing='0'>
<caption>員工信息表</caption>
<tr id="title">
<th>編號(hào)</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>工資</th>
<th>郵箱</th>
<th>職位</th>
<th>負(fù)責(zé)區(qū)域</th>
<th>操作</th>
</tr>
</table>
<!--頁碼部分 -->
<div id='page'>
</div>
</main>
</body>
<script>
//頁面主體內(nèi)容獲取和生成函數(shù)
function getrows(data) {
//如果頁面已經(jīng)記錄信息需要移除
if ($($("table>tbody")[0]).children().length > 1) {
$($("table>tbody")[0]).children().remove("tr:not(#title)");
};
//生成html內(nèi)容
var content = "";
data.forEach((item) => {
content += `<tr>
<td>${item["id"]}</td>
<td>${item["name"]}</td>
<td>${item["age"]}</td>
<td>${item["gender"]}</td>
<td>${item["salary"]}</td>
<td>${item["email"]}</td>
<td>${item["postion"]}</td>
<td>${item["area"]}</td>
<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>
</tr>
`;
});
//渲染到頁面中取
$('#title').after(content);
}
//生成分條條函數(shù)
function getpage(pages) {
//如果沒有分頁條則生成
if ($("#page").children().length == 0) {
var plink = "<span>首頁</span><span><</span>";
for (var i = 1; i <= pages; i++) {
plink += `<span>${i}</span>`;
}
plink += "<span>></span><span>尾頁</span>"
// console.log(plink);
$("#page").append(plink);
}
// console.log($("#page").children().length);
}
//ajax獲取數(shù)據(jù)信息函數(shù)
function getdata(page) {
$.ajax({
type: 'GET',
url: 'http://php.edu/test/staffs_api.php',
data: {
p: page
},
dataType: 'json',
success: function(res) {
// console.log(res);
var data = res['data'];
var pages = res['pages'];
// console.log(data, pages);
//調(diào)用函數(shù)生成頁面信息
getrows(data);
getpage(pages);
}
});
}
//首次打開頁面默認(rèn)獲取首頁數(shù)據(jù)信息并展示
window.onload = function() {
getdata(1);
}
//通過委托事件實(shí)現(xiàn)翻頁(上一頁下一頁功能沒有實(shí)現(xiàn))
$('#page').click(function(ev) {
// console.log(ev.target.textContent, ev.currentTarget);
var currentpage = ev.target.textContent;
switch (currentpage) {
case "首頁":
getdata(1);
break;
case "尾頁":
var count = $(ev.currentTarget).children().length - 4;
getdata(count);
break;
case "<":
console.log('上一頁');
break;
case ">":
console.log("下一頁");
break;
default:
getdata(currentpage);
}
});
//刪除數(shù)據(jù)功能,通過ajaxComplete()函數(shù)來獲取動(dòng)態(tài)生成的信息
$(document).ajaxComplete(function() {
// console.log($('.delete'));
$('.delete').click(function(ev) {
var id = $(ev.target).data("index");
// console.log(id);
if (window.confirm("確定刪除嗎?")) {
$.ajax({
type: 'GET',
url: 'http://php.edu/test/delete.php',
data: {
id: id
},
dataType: 'json',
success: function(res) {
if (res[0] > 0) {
return alert("刪除成功");
} else {
return alert("刪除失敗");
}
}
});
window.location.reload();
} else {
return false;
}
});
});
</script>
<style>
main {
width: 1000px;
margin: 10px auto;
}
table {
width: 1000px;
}
#page {
margin-top: 10px;
width: 1000px;
display: flex;
justify-content: space-around;
}
#page>span {
width: 46px;
height: 20px;
background-color: lightgray;
/* border: 1px solid black; */
text-align: center;
color: whitesmoke;
line-height: 20px;
}
#page>span:hover {
cursor: pointer;
background-color: white;
color: red;
}
th,
td {
text-align: center;
}
</style>
</html>
(一)功能實(shí)現(xiàn)原理
1.通過分頁的編輯按鈕獲取要編輯數(shù)據(jù)記錄的id,然后通過編輯頁面把要編輯的記錄顯示出來,
2.通過ajax獲取新的數(shù)據(jù),通過id,更新信息
(二)實(shí)現(xiàn)代碼
1.前端代碼
<?php
include('connection.php');
$sql="select * from v_staffs where id=:id;";
$stmt=$pdo->prepare($sql);
$id=$_GET["id"];
$stmt->bindParam(':id',$id,PDO::PARAM_INT);
$stmt->execute();
// $stmt->debugDumpParams();
$row=$stmt->fetch();
// var_dump($row);
// exit();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>員工信息修改</title>
</head>
<body>
<body>
<table border="1" cellpadding='3' cellspacing='0'>
<caption>修改員工信息</caption>
<tr>
<th>編號(hào)</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>工資</th>
<th>郵箱</th>
</tr>
<tr>
<td id="id"><?= $row["id"] ?></td>
<td><input type="text" name="name" value="<?= $row["name"] ?>"></td>
<td><input type="text" name="age" value="<?= $row["age"] ?>"></td>
<td><input type="text" name="gender" value="<?= $row["gender"] ?>"></td>
<td><input type="text" name="salary" value="<?= $row["salary"] ?>"></td>
<td><input type="text" name="email" value="<?= $row["email"] ?>"></td>
</tr>
</table>
<button id="update">提交</button>
</body>
<script>
$("#update").click(function(){
$.ajax({
type: 'POST',
url: 'http://php.edu/test/update.php',
data: {
//獲取新數(shù)據(jù)
name: $("[name='name']").val(),
age:$("[name='age']").val(),
gender:$("[name='gender']").val(),
salary:$("[name='salary']").val(),
email:$("[name='email']").val(),
id:$("#id").text()
},
dataType: 'json',
success: function(res) {
//確認(rèn)更新成功
alert(res[1]);
window.close();//關(guān)閉窗口
}
});
});
</script>
</html>
2.后端代碼
<?php
include('connection.php');
$sql="update staffs set name=?,age=?,gender=?,salary=?,email=? where id=?;";
$stmt=$pdo->prepare($sql);
$stmt->execute([$_POST['name'],$_POST["age"],$_POST["gender"],$_POST["salary"],$_POST["email"],$_POST['id']]);
// $stmt->debugDumpParams();
if($stmt->rowCount()>0){
echo json_encode([$stmt->rowCount(),"更新成功"]);
}else{
echo json_encode([0,"更新失敗"]);
}
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)