-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit_post.php
106 lines (71 loc) · 1.92 KB
/
edit_post.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
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
99
100
101
102
103
104
105
106
<?php
include_once('resources/init.php');
$post = get_posts($_GET['id']);
if (isset($_POST['title'], $_POST['contents'], $_POST['category'])) {
$errors = array();
$title = trim($_POST['title']);
$contents = trim($_POST['contents']);
if (empty($title)) {
$errors[] = 'You must submit some title';
}
if (empty($contents)) {
$errors[] = 'You must enter some text here';
}
if (! category_exists('id', $_POST['category'])) {
$errors[] = "That category does not exist";
}
if (strlen($title) > 255) {
$errors[] = "The title cannot be longer than 255 characters ";
}
if (empty($errors) ) {
edit_post($_GET['id'], $title, $contents, $_POST['category']);
header("Location: index.php?id={$post[0]['post_id']}");
die();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset ="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<style>
label{ display: block;}
</style>
<title> Edit a Post </title>
</head>
<body>
<h1>Edit a Post</h1>
<?php
if (isset($errors) && ! empty($errors)) {
echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
}
?>
<form action="" method="Post"></form>
<div>
<label for="title">Title</label>
<input type="text" name="title" value="<?php echo $post[0]['title']; ?>">
</div>
<div>
<label for="contents">Contents</label>
<textarea name="contents" rows="15" cols="50"> <?php echo $post[0]['contents']; ?></textarea>
</div>
<div>
<label for="category">Category</label>
<select name="category">
<?php
foreach (get_categories() as $category) {
$selected = ($category['names'] == $post[0]['names']) ? 'selected' : '' ;
?>
<option value="<?php echo $category['id']; ?>" <?php echo $selected; ?>> <?php echo $category['names']; ?> </option>
<?php
}
?>
</select>
</div>
<div>
<input type="submit" value="Edit Post">
</div>
</form>
</body>
</html>