Guest Book
This commit is contained in:
parent
5adfb13d83
commit
54e43f4318
45
tasks/guestBook/comment.php
Normal file
45
tasks/guestBook/comment.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
include_once "../../base/errors.php";
|
||||
include_once "../../base/headers.php";
|
||||
|
||||
session_start();
|
||||
$_SESSION["error"] = [];
|
||||
|
||||
if (!isset($_POST["title"])) {
|
||||
array_push($_SESSION["error"], "Title was not among the data sent.");
|
||||
}
|
||||
if (!isset($_POST["name"])) {
|
||||
array_push($_SESSION["error"], "Name was not among the data sent.");
|
||||
}
|
||||
if (!isset($_POST["comment"])) {
|
||||
array_push($_SESSION["error"], "Comment was not among the data sent.");
|
||||
}
|
||||
|
||||
$title = trim($_POST["title"]);
|
||||
$name = trim($_POST["name"]);
|
||||
$comment = substr(trim($_POST["comment"]), 0, 250);
|
||||
$time = time();
|
||||
|
||||
if ($title === "") {
|
||||
array_push($_SESSION["error"], "The title was empty.");
|
||||
}
|
||||
if ($name === "") {
|
||||
array_push($_SESSION["error"], "The name was empty.");
|
||||
}
|
||||
if ($comment === "") {
|
||||
array_push($_SESSION["error"], "The comment was empty.");
|
||||
}
|
||||
|
||||
if (count($_SESSION["error"]) > 0) {
|
||||
Headers::redirect(".");
|
||||
return;
|
||||
}
|
||||
|
||||
$string = file_get_contents("data.json") ?? "[]";
|
||||
$json = json_decode($string);
|
||||
|
||||
array_push($json, ["time" => $time, "title" => $title, "name" => $name, "comment" => $comment]);
|
||||
|
||||
file_put_contents("data.json", json_encode($json));
|
||||
|
||||
Headers::redirect(".");
|
||||
1
tasks/guestBook/data.json
Normal file
1
tasks/guestBook/data.json
Normal file
@ -0,0 +1 @@
|
||||
[{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715337098,"title":"The first","name":"Kilian","comment":"The first comment on here."},{"time":1715338181,"title":"","name":"asd","comment":"sad"},{"time":1715338381,"title":"sdf","name":"dsaf","comment":"dsg"},{"time":1715339761,"title":"Witzige Aufgabe","name":"Kilian","comment":"nun ja, also das funktioniert ja schon recht gut gell."}]
|
||||
22
tasks/guestBook/formValidation.js
Normal file
22
tasks/guestBook/formValidation.js
Normal file
@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const handler = (event) => {
|
||||
if (!event.target.checkValidity()) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
event.target.classList.add("was-validated");
|
||||
};
|
||||
|
||||
const addValidation = () => {
|
||||
const forms = document.querySelectorAll(".needs-validation");
|
||||
|
||||
Array.from(forms).forEach((form) => {
|
||||
form.addEventListener("submit", handler, false);
|
||||
});
|
||||
};
|
||||
|
||||
addValidation();
|
||||
});
|
||||
146
tasks/guestBook/index.php
Normal file
146
tasks/guestBook/index.php
Normal file
@ -0,0 +1,146 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
|
||||
<title>Guest Book</title>
|
||||
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
|
||||
<script src="formValidation.js"></script>
|
||||
<script src="textarea.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<?php
|
||||
include_once "../../base/errors.php";
|
||||
include_once "../../base/headers.php";
|
||||
|
||||
session_start();
|
||||
$errors = $_SESSION["error"] ?? [];
|
||||
$_SESSION["error"] = [];
|
||||
|
||||
$string = file_get_contents("data.json") ?? "[]";
|
||||
$json = json_decode($string);
|
||||
|
||||
if (isset($_GET["p"]) && !is_numeric($_GET["p"])) {
|
||||
Headers::redirect(".");
|
||||
return;
|
||||
}
|
||||
|
||||
$page = isset($_GET["p"]) ? intval($_GET["p"]) : 0;
|
||||
$maxPage = intdiv(count($json), 9);
|
||||
if ($page > $maxPage) {
|
||||
Headers::redirect("./?p=$maxPage");
|
||||
return;
|
||||
}
|
||||
|
||||
$data = array_slice($json, $page * 9, 9);
|
||||
|
||||
Headers::html();
|
||||
?>
|
||||
|
||||
<div class="container-fluid p-0">
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand" href="#">Guest Book</a>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
|
||||
<symbol id="exclamation-triangle-fill" fill="currentColor" viewBox="0 0 16 16">
|
||||
<path d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767L8.982 1.566zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5zm.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2z" />
|
||||
</symbol>
|
||||
</svg>
|
||||
|
||||
<div class="row m-0">
|
||||
<div class="col">
|
||||
<?php foreach ($errors as $error) { ?>
|
||||
<div class="alert alert-danger alert-dismissible fade show mt-3 mb-0">
|
||||
<svg class="bi flex-shrink-0 me-2" width="24" height="24">
|
||||
<use xlink:href="#exclamation-triangle-fill" />
|
||||
</svg>
|
||||
<?= $error ?>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||
</div>
|
||||
<? } ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row row-cols-1 row-cols-lg-3 g-4 m-0 mb-3">
|
||||
<?php foreach ($data as $comment) { ?>
|
||||
<div class="col">
|
||||
<div class="card h-100">
|
||||
<div class="card-header">
|
||||
<h5 class="card-title"><?= $comment->title ?></h5>
|
||||
<h6 class="card-subtitle mb-2 text-muted"><?= $comment->name ?></h6>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<p class="card-text"><?= $comment->comment ?></p>
|
||||
</div>
|
||||
<div class="card-footer">
|
||||
<small class="text-muted">
|
||||
<?php echo date("d.m.Y H:i", $comment->time) ?>
|
||||
</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
|
||||
<nav>
|
||||
<ul class="pagination justify-content-center">
|
||||
<li class="page-item">
|
||||
<a class="page-link <?php if ($page === 0) echo "disabled" ?>" href="?p=<?= $page > 0 ? $page - 1 : 0 ?>">
|
||||
<span aria-hidden="true">«</span>
|
||||
</a>
|
||||
</li>
|
||||
<?php for ($i = 0; $i <= $maxPage; $i++) { ?>
|
||||
<li class="page-item <?php if ($page === $i) echo "active" ?>">
|
||||
<a class="page-link" href="?p=<?= $i ?>"><?= $i + 1 ?></a>
|
||||
</li>
|
||||
<?php } ?>
|
||||
<li class="page-item">
|
||||
<a class="page-link <?php if ($page === $maxPage) echo "disabled" ?>" href="?p=<?= $page < $maxPage ? $page + 1 : $maxPage ?>">
|
||||
<span aria-hidden="true">»</span>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
<hr />
|
||||
|
||||
<form action="comment.php" method="post" class="needs-validation row m-0" novalidate>
|
||||
<div class="col">
|
||||
<h4 class="mb-3">Leave a Comment!</h4>
|
||||
<div class="mb-3">
|
||||
<input class="form-control" type="text" name="title" required placeholder="A catching title" />
|
||||
<div class="invalid-feedback">
|
||||
Please enter a title.
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<input class="form-control" type="text" name="name" required placeholder="Your name" />
|
||||
<div class="invalid-feedback">
|
||||
Please enter a name.
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 position-relative">
|
||||
<textarea class="form-control" id="comment" name="comment" rows="3" maxlength="250" required placeholder="Comment here"></textarea>
|
||||
<small class="text-muted position-absolute" id="comment-count" style="right: 20px; bottom: 0;">
|
||||
0/250
|
||||
</small>
|
||||
<div class="invalid-feedback">
|
||||
Please enter text.
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-primary mb-3">Comment</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
28
tasks/guestBook/textarea.js
Normal file
28
tasks/guestBook/textarea.js
Normal file
@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const handler = (event) => {
|
||||
console.log(event.target);
|
||||
const length = event.target.value.length;
|
||||
const maxLength = event.target.maxLength;
|
||||
const counter = document.getElementById("comment-count");
|
||||
|
||||
counter.innerText = `${length}/${maxLength}`;
|
||||
|
||||
if (length > maxLength - 50) {
|
||||
counter.classList.remove("text-muted");
|
||||
counter.classList.add("text-danger");
|
||||
} else {
|
||||
counter.classList.remove("text-danger");
|
||||
counter.classList.add("text-muted");
|
||||
}
|
||||
};
|
||||
|
||||
const addValidation = () => {
|
||||
const textarea = document.getElementById("comment");
|
||||
|
||||
textarea.addEventListener("keyup", handler);
|
||||
};
|
||||
|
||||
addValidation();
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user