-
Notifications
You must be signed in to change notification settings - Fork 0
/
forum.html
61 lines (55 loc) · 1.95 KB
/
forum.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Forum_page</title>
<link rel="stylesheet" type="text/css" href="/styles.css">
</head>
<body>
<section class="forum-section">
<h3 id="welcome-message">Welcome to forum!</h3>
<br>
<p>
Here you can see what topics there are so far.
You can create a new post by fill in your name, title and content.
To read a post's content or send a comment - click read more!
</p>
<!-- Form create new post -->
<form class="new-post-form" id="new-post-form" action="/addpost" method="post">
<input type="text" name="author" placeholder="Your Name" required />
<input type="text" name="heading" placeholder="Title" required />
<textarea name="body" placeholder="Content" required></textarea>
<button type="submit">Create Post</button>
</form>
<hr>
<!-- Display all posts -->
<div id="post-container"></div>
</section>
<script>
// redirect to the view post page
function viewPost(postId) {
window.location.href = `/viewpost/${postId}`;
}
document.addEventListener("DOMContentLoaded", function () {
// Fetch posts and render on page forum
fetch("/getposts")
.then((response) => response.json())
.then((posts) => {
const postsContainer = document.getElementById("post-container");
posts.forEach((post) => {
const postElement = document.createElement("div");
postElement.innerHTML = `
Author:${post.author}<br>
<strong>${post.heading}</strong><br>
Created at: ${new Date(post.created_at).toLocaleString()}<br>
<button onclick="viewPost(${post.post_id})">Read more</button>
<hr>
`;
postsContainer.appendChild(postElement);
});
});
});
</script>
</body>
</html>