ajax implementiert
This commit is contained in:
@@ -114,7 +114,9 @@ if (isset($_GET["id"])) {
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
<p>Noch keine Kommentare vorhanden.</p>
|
||||
<p class="no-comments-message">
|
||||
Noch keine Kommentare vorhanden.
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -29,14 +29,14 @@ if ($pfad === "deleteAccount") {
|
||||
}
|
||||
?>
|
||||
|
||||
<!--
|
||||
<!--
|
||||
Seite: Index der Lernplattform
|
||||
Funktion: Webseitengerüst, Anzeigen von Content
|
||||
-->
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
-->
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
|
||||
<head>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="description" content="EduForge">
|
||||
<meta name="author" content="Niklas Ortmann">
|
||||
@@ -52,32 +52,7 @@ if ($pfad === "deleteAccount") {
|
||||
<link rel="stylesheet" href="css/showArticle.css">
|
||||
<link rel="stylesheet" href="css/message.css">
|
||||
|
||||
<script src="js/paginator.js" async></script>
|
||||
<script src="js/comments.js" defer></script>
|
||||
|
||||
<title>EduForge</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<?php
|
||||
include_once 'includes/navbar.php';
|
||||
|
||||
/*
|
||||
Dynamischer Inhalt:
|
||||
Je nach pfad-Parameter wird die passende Datei aus content geladen.
|
||||
*/
|
||||
if (file_exists('content/' . $pfad . '.php')) {
|
||||
include_once 'content/' . $pfad . '.php';
|
||||
} else {
|
||||
include_once 'content/404.php';
|
||||
}
|
||||
|
||||
include_once 'includes/footer.php';
|
||||
?>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
<?php
|
||||
ob_end_flush();
|
||||
?>
|
||||
</head>
|
||||
@@ -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 = `
|
||||
<p>
|
||||
<strong>${escapeHtml(data.author)}</strong>
|
||||
<span>${escapeHtml(data.created)}</span>
|
||||
</p>
|
||||
<p>${escapeHtml(data.content).replace(/\n/g, "<br>")}</p>
|
||||
`;
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
header("Content-Type: application/json");
|
||||
|
||||
require_once "../model/CommentManager.php";
|
||||
|
||||
if (!isset($_SESSION["user_email"])) {
|
||||
echo json_encode([
|
||||
"success" => 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."
|
||||
]);
|
||||
}
|
||||
Reference in New Issue
Block a user