院線片的編輯表單和新增是一樣的,可以透過複製來完成,同時後端的部份可以利用$_POST中是否有id來判斷是要新增還是編輯,藉此減少後端程式的重覆性。
編輯電影
- 在編輯電影的按鈕中有設定要編輯電影的id,因此在載入編輯電影檔案時可以利用id來取得電影的資料
- 建立
/back/edit_movie.php
檔案,並撰寫表單html碼,由於表單欄位和新增電影是一樣的,因此可以直接從新增電影複製過來使用 - 將新增電影表單中的id屬性都改為value,並填入對應的電影資料
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98<style>
.form td:nth-child(1){
text-align-last: justify;
padding:3px 5px;
}
</style>
<h2 class="ct">編輯院線片</h2>
<!--利用網址帶的id取得電影資料-->
<?php $movie=$Movie->find($_GET['id']);?>
<form action="./api/save_movie.php" method="post" enctype="multipart/form-data">
<div style="display:flex;align-items:start">
<div style="width:15%;">影片資料</div>
<div style="width:85%;">
<table class="ts form">
<tr>
<td class="ct" width="20%">片名</td>
<td><input type="text" name="name" value="<?=$movie['name'];?>"></td>
</tr>
<tr>
<td class="ct">分級</td>
<td>
<select name="level" id="">
<option value="1" <?=($movie['level']==1)?'selected':'';?>>普遍級</option>
<option value="2" <?=($movie['level']==2)?'selected':'';?>>輔導級</option>
<option value="3" <?=($movie['level']==3)?'selected':'';?>>保護級</option>
<option value="4" <?=($movie['level']==4)?'selected':'';?>>限制級</option>
</select>
</td>
</tr>
<tr>
<td class="ct">片長</td>
<td><input type="text" name="length" value="<?=$movie['length'];?>"></td>
</tr>
<tr>
<td class="ct">上映日期</td>
<td>
<?php
//利用解構賦值特性,將年月日資料拆成三個不同的變數
[$year,$month,$date]=explode("-",$movie['ondate']);
?>
<select name="year" id="">
<!--利用判斷式來決定選項要定位在那一年-->
<option value="2024" <?=($year==2024)?'selected':'';?>>2024</option>
<option value="2025" <?=($year==2025)?'selected':'';?>>2025</option>
</select>年
<select name="month" id="">
<?php
for($i=1;$i<=12;$i++){
//利用判斷式來決定選項要定位在那一月
$selected= ($month==$i)?'selected':'';
echo "<option value='$i' $selected>$i</option>";
}
</select>月
<select name="date" id="">
for($i=1;$i<=31;$i++){
//利用判斷式來決定選項要定位在那一天
$selected= ($date==$i)?'selected':'';
echo "<option value='$i' $selected>$i</option>";
}
</select>日
</td>
</tr>
<tr>
<td class="ct">發行商</td>
<td><input type="text" name="publish" value="<?=$movie['publish'];?>"></td>
</tr>
<tr>
<td class="ct">導演</td>
<td><input type="text" name="director" value="<?=$movie['director'];?>"></td>
</tr>
<tr>
<td class="ct">預告影片</td>
<td><input type="file" name="trailer" id=""></td>
</tr>
<tr>
<td class="ct">電影海報</td>
<td><input type="file" name="poster" id=""></td>
</tr>
</table>
</div>
</div>
<div style="display:flex;align-items:start">
<div style="width:15%;">劇情簡介</div>
<div style="width:85%;">
<textarea name="intro" style="width:99%;height:100px;"><?=$movie['intro'];?></textarea>
</div>
</div>
<div class="ct">
<!--增加一個隱藏欄位id,讓後端知道這是那一筆資料-->
<input type="hidden" name="id" value="<?=$movie['id'];?>">
<input type="submit" value="編輯">
<input type="reset" value="重置">
</div>
</form> - 利用新增的程式
/api/edit_movie.php
來完成編輯功能,不用寫新的檔案
刪除電影
刪除電影的做法一樣使用ajax的方式來進行,只需要傳遞table名和id給後端就可以了,後端刪除完畢後,前端再重整頁面,就可以看到刪除的結果
/back/movie.php
1 | $(".del-btn").on("click",function(){ |
/view/backend/del.php
1 | include_once "db.php"; |