From e1ee70b46f60e97ad5733f02844d9ed2472c1c8b Mon Sep 17 00:00:00 2001 From: Caroline Schulte Date: Mon, 15 Jun 2026 21:54:06 +0200 Subject: [PATCH] ajax implementiert --- content/showArticle.php | 4 ++- index.php | 71 +++++++++++++--------------------------- js/comments.js | 55 +++++++++++++++++++++++++++++++ php/ajax/add-comment.php | 48 +++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 49 deletions(-) create mode 100644 js/comments.js create mode 100644 php/ajax/add-comment.php diff --git a/content/showArticle.php b/content/showArticle.php index fff5b00..3feb0bf 100644 --- a/content/showArticle.php +++ b/content/showArticle.php @@ -114,7 +114,9 @@ if (isset($_GET["id"])) { -

Noch keine Kommentare vorhanden.

+

+ Noch keine Kommentare vorhanden. +

diff --git a/index.php b/index.php index 1006322..5b5a7cb 100644 --- a/index.php +++ b/index.php @@ -29,55 +29,30 @@ if ($pfad === "deleteAccount") { } ?> - - - + + + - - - - - - + + + + + + - - - - - - - - + + + + + + + + - + - EduForge - - - - - - - - - - \ No newline at end of file + EduForge + \ No newline at end of file diff --git a/js/comments.js b/js/comments.js new file mode 100644 index 0000000..7ee43f7 --- /dev/null +++ b/js/comments.js @@ -0,0 +1,55 @@ +document.addEventListener("DOMContentLoaded", function () { + const form = document.getElementById("comment-form"); + const commentsList = document.getElementById("comments-list"); + const commentContent = document.getElementById("comment-content"); + + if (!form || !commentsList || !commentContent) { + return; + } + + form.addEventListener("submit", function (event) { + event.preventDefault(); + + const formData = new FormData(form); + + fetch("php/ajax/add-comment.php", { + method: "POST", + body: formData + }) + .then(response => response.json()) + .then(data => { + if (!data.success) { + alert(data.message); + return; + } + + const emptyMessage = commentsList.querySelector(".no-comments-message"); + if (emptyMessage) { + emptyMessage.remove(); + } + + const commentElement = document.createElement("div"); + commentElement.classList.add("comment-item"); + + commentElement.innerHTML = ` +

+ ${escapeHtml(data.author)} + ${escapeHtml(data.created)} +

+

${escapeHtml(data.content).replace(/\n/g, "
")}

+ `; + + commentsList.prepend(commentElement); + commentContent.value = ""; + }) + .catch(() => { + alert("Kommentar konnte nicht gesendet werden."); + }); + }); + + function escapeHtml(text) { + const div = document.createElement("div"); + div.textContent = text; + return div.innerHTML; + } +}); \ No newline at end of file diff --git a/php/ajax/add-comment.php b/php/ajax/add-comment.php new file mode 100644 index 0000000..0895de3 --- /dev/null +++ b/php/ajax/add-comment.php @@ -0,0 +1,48 @@ + false, + "message" => "Du musst angemeldet sein." + ]); + exit(); +} + +$articleId = $_POST["article_id"] ?? null; +$content = trim($_POST["content"] ?? ""); + +if (empty($articleId) || empty($content)) { + echo json_encode([ + "success" => false, + "message" => "Kommentar darf nicht leer sein." + ]); + exit(); +} + +try { + $commentManager = CommentManager::getInstance(); + + $commentManager->addComment( + $articleId, + $_SESSION["user_email"], + $content + ); + + echo json_encode([ + "success" => true, + "author" => $_SESSION["user_email"], + "content" => $content, + "created" => date("Y-m-d H:i:s") + ]); + +} catch (Exception $e) { + echo json_encode([ + "success" => false, + "message" => "Kommentar konnte nicht gespeichert werden." + ]); +}