Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/projekt/slutprojekt/
board_random.php
board_travel.php
board_vgy.php
create_comment.php
create_post.php
create_tables.php
darkmode.js
fetch_comments.php
fetch_posts.php
fetch_posts_random.php
forgot_password.php
index.php
login.php
nav.css
nav.php
post.php
register.php
reset_password.php
sql_inject.php
verify.php
board_random.php
109 lines UTF-8 Windows (CRLF)
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
107
108
109
<?php
session_start();
include ('../../incl/dbconnect.php');
// hämta eller skapa board random
$stmt = $dbconn->prepare("SELECT board_id FROM Boards_slutprojekt WHERE board_name = :name");
$stmt->execute([':name' => 'random']);
$board = $stmt->fetch(PDO::FETCH_ASSOC);
if ($board) {
$board_id = $board['board_id'];
} else {
$stmt = $dbconn->prepare("INSERT INTO Boards_slutprojekt (board_name) VALUES (:name)");
$stmt->execute([':name' => 'random']);
$board_id = $dbconn->lastInsertId();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Random Board</title>
<link rel="stylesheet" href="nav.css">
<style>
.post { border:1px solid #ccc; padding:10px; margin-bottom:10px; }
</style>
</head>
<body>
<?php include ('nav.php'); ?>
<h1>Random Board</h1>
<h2>Skapa ett nytt inlägg</h2>
<?php if(!isset($_SESSION['user_id'])): ?>
<p><a href="login.php">Logga in</a> för att posta.</p>
<?php else: ?>
<form id="postForm" method="POST" action="create_post.php" enctype="multipart/form-data">
<textarea name="content" placeholder="Skriv ditt inlägg" required rows="5" cols="50"></textarea><br>
<input type="file" name="image_file" accept="image/*"><br>
<input type="hidden" name="board_id" value="<?php echo $board_id; ?>">
<button type="submit">Skicka inlägg</button>
</form>
<?php endif; ?>
<h2>Inlägg</h2>
<div id="postsContainer">
<!-- AJAX kommer fylla på här -->
</div>
<script>
// hämta inlägg i JSON och rendera
function fetchPosts() {
fetch('fetch_posts.php?board_id=<?php echo $board_id; ?>')
.then(r => r.json())
.then(data => {
const c = document.getElementById('postsContainer');
c.innerHTML = '';
data.forEach(post => {
const imgTag = post.image_url
? `<img src="${post.image_url}" style="max-width:200px;"><br>`
: '';
const star = post.post_count >= 5 ? ' 🌟' : '';
const commentsInfo = `<p><small>${post.comment_count} kommentar${post.comment_count === 1 ? '' : 'er'}</small></p>`;
const html = `
<div class="post">
<p><strong>${post.username}${star}</strong> skrev:</p>
<p>${post.content}</p>
${imgTag}
${commentsInfo}
<p><small>${post.created_at}</small></p>
<p><a href="post.php?post_id=${post.post_id}">Se kommentarer</a></p>
</div>
`;
c.insertAdjacentHTML('beforeend', html);
});
})
.catch(err => console.error('Fel vid hämtning av inlägg:', err));
}
// AJAX-formulärhantering
const form = document.getElementById('postForm');
if(form){
form.addEventListener('submit', e=>{
e.preventDefault();
const fd = new FormData(form);
fetch('create_post.php', { method:'POST', body:fd })
.then(r=>r.text())
.then(txt=>{
console.log(txt);
form.reset();
fetchPosts();
})
.catch(e=>console.error(e));
});
}
// auto uppdatering
document.addEventListener('DOMContentLoaded', ()=>{ fetchPosts(); setInterval(fetchPosts,3000); });
</script>
<script src="darkmode.js"></script>
</body>
</html>