-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-post.php
More file actions
89 lines (79 loc) · 2.84 KB
/
Copy pathcreate-post.php
File metadata and controls
89 lines (79 loc) · 2.84 KB
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
<?php
require_once 'lib/common.php';
require_once 'lib/edit-post.php';
if (!isLoggedIn()) {
redirectAndExit('login.php');
}
$pdo = connectDatabase();
$title = '';
$body = '';
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = trim($_POST['post-title'] ?? '');
$body = trim($_POST['post-body'] ?? '');
if (!$title || !$body) {
$errors[] = 'Both title and body are required.';
}
if (empty($errors)) {
$userId = getAuthUserId();
$postId = addPost($pdo, $title, $body, $userId);
if ($postId === false) {
$errors[] = 'Failed to create post.';
} else {
redirectAndExit('index.php');
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create New Post</title>
<style>
body { font-family: Arial, sans-serif; background: #f4f4f4; margin: 0; padding: 0; }
.container { max-width: 800px; margin: 50px auto; background: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; font-weight: bold; }
input[type="text"], textarea { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 16px; }
input[type="submit"] { padding: 12px 20px; border: none; border-radius: 4px; background-color: #4CAF50; color: #fff; font-size: 16px; cursor: pointer; }
input[type="submit"]:hover { background-color: #45a049; }
.error { color: red; margin-bottom: 15px; }
</style>
</head>
<header>
<?php
if (file_exists('templates/title.php')) {
require 'templates/title.php';
} else {
echo '<h1>My Blog</h1>';
}
?>
</header>
<body>
<div class="container">
<h1>Create New Post</h1>
<?php if ($errors): ?>
<div class="error">
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo escapeHtml($error); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="post">
<div class="form-group">
<label for="post-title">Title:</label>
<input type="text" id="post-title" name="post-title" value="<?php echo escapeHtml($title); ?>" required placeholder="Enter the post title">
</div>
<div class="form-group">
<label for="post-body">Body:</label>
<textarea id="post-body" name="post-body" rows="12" required placeholder="Write your post here"><?php echo escapeHtml($body); ?></textarea>
</div>
<input type="submit" value="Create Post">
</form>
</div>
</body>
</html>