0%

[技能檢定]題組二 步驟12 後台頁面及後台帳號管理

題組二的後台相較題組一簡單許多,甚至沒有新增文章的功能,所以我們需要手動先把文章匯入到資料庫去,也因此,所以題組二的後台容易拿分,可以安排在會員註冊登入完成後就先來製作後台的功能,確認這部份的分數是有先到手的。

  1. 依題意,後台有一個提示選擇管理項目的頁面,我們直接把這段文字放在 ./back/main.php 中,使用 H1 級的標籤及置中的 class 就可以了

    1
    <h1 class='ct'>請選擇管理功能</h1>
  2. 依題意,修改 back.php 左側選單的文字和連結

    1
    2
    3
    4
    5
    6
    7
    <div class="hal" id="lef">
    <a class="blo" href="?do=admin">帳號管理</a>
    <a class="blo" href="?do=po">分類網誌</a>
    <a class="blo" href="?do=news">最新文章管理</a>
    <a class="blo" href="?do=know">講座管理</a>
    <a class="blo" href="?do=que">問卷管理</a>
    </div>
  3. 建立 /back/admin.php 檔案來製作帳號管理功能

建立帳號管理頁面

/back/admin.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<fieldset>
<legend>帳號管理</legend>
<form action="./api/edit_user.php" method="post">
<table style="width:55%;margin:auto;text-align:center">
<tr>
<td class='clo'>帳號</td>
<td class='clo'>密碼</td>
<td class='clo'>刪除</td>
</tr>
<?php
$rows=$User->all();
foreach($rows as $row){
if($row['acc']!='admin'){
?>
<tr>
<td><?=$row['acc'];?></td>
<td><?=str_repeat("*", mb_strlen($row['pw']));?></td>
<td>
<input type="checkbox" name="del[]" value="<?=$row['id'];?>">
</td>
</tr>
<?php
}
}
?>
</table>
<div class="ct">
<input type="submit" value="確定刪除">
<input type="reset" value="清空選取">
</div>
</form>
</fieldset>

撰寫後端處理帳號刪除程式

1
2
3
4
5
6
7
8
9
10
include_once "db.php";
//判斷是否有需要刪除的動作
if(isset($_POST['del'])){
//使用迴圈來刪除指定id的帳號資料
foreach($_POST['del'] as $id){
$User->del($id);
}
}
//返回會員管理頁面
to("../back.php?do=admin");

建立新增會員頁面

由於後台的新增會員和前台的註冊會員是幾乎一樣的功能,因此可以直接把註冊會員的功能直接複製過來使用即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<h2>新增會員</h2>
<span style="color:red">*請設定您要註冊的帳號及密碼(最長12個字元)</span>
<table>
<tr>
<td class="clo">Step1:登入帳號</td>
<td><input type="text" name="acc" id="acc"></td>
</tr>
<tr>
<td class="clo">Step2:登入密碼</td>
<td><input type="password" name="pw" id="pw"></td>
</tr>
<tr>
<td class="clo">Step3:再次確認密碼</td>
<td><input type="password" name="pw2" id="pw2"></td>
</tr>
<tr>
<td class="clo">Step4:信箱(忘記密碼時使用)</td>
<td><input type="text" name="email" id="email"></td>
</tr>
<tr>
<td>
<input type="button" value="註冊" onclick="reg()">
<input type="reset" value="清除">
</td>
<td></td>
</tr>
</table>

新增會員的js和後端php的部份可以延用註冊會員的程式即,但是題目在後台沒有提到需要出現彈出視窗提示,因此這邊我們可以在js上做點小修改,讓註冊完成直接重整畫面,就可以在上面的帳號管理中馬上看到新增的會員

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function reg(){
let user={acc:$("#acc").val(),
pw:$("#pw").val(),
pw2:$("#pw2").val(),
email:$("#email").val()
}
if(user.acc!='' && user.pw!='' && user.pw2!='' && user.email!=''){
if(user.pw==user.pw2){
$.post("./api/chk_acc.php",{acc:user.acc},(res)=>{
//console.log(res)
if(parseInt(res)==1){
alert("帳號重覆")
}else{
$.post('./api/reg.php',user,(res)=>{
//註冊成功時直接重整頁面,不用跳出註冊成功通知
location.reload()
})
}
})
}else{
alert("密碼錯誤")
}
}else{
alert("不可空白")
}
}