focus comment after posting

This commit is contained in:
Zdenek Borovec 2024-05-06 20:20:57 +02:00
parent c8caf0dfac
commit 60d2a67620

View file

@ -19,7 +19,7 @@ class BlogpostComment
*/
public function display_comment() {
printf("
<div class=\"comment\">
<div class=\"comment\" id=\"comment-%s\">
<div class=\"comment-own-wrapper\">
<span class=\"comment-author\"> By: %s </span>
<span class=\"comment-date\"> On: %s </span>
@ -45,7 +45,9 @@ class BlogpostComment
</div>
<div class=\"comment-child-wrapper\">
",
$this->poster_name, date("Y-m-d H:i", strtotime($this->timestamp)),
$this->comment_id,
$this->poster_name,
date("Y-m-d H:i", strtotime($this->timestamp)),
$this->comment_id,
$this->comment_id,
$this->content,
@ -232,15 +234,27 @@ class Blogpost
* If the poster is not signed in, send "NULL" (as a string) as the $posterID
* The same goes for $parentId (that is the parent comment,
* if this one is a response)
* Returns: error message string or null on success.
* Returns: GUID PK of the newly added comment.
*/
function send_comment($conn, $blogId, $posterId, $content, $parentId) {
// If content is empty, do not post
if(empty($content)) {
return "";
}
// Get a uuid for the comment
$stmt = $conn->prepare("SELECT UUID()");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$uuid = $result["UUID()"];
// Prepare the statemtnt
$stmt = $conn->prepare("INSERT INTO blogpost_comments
( parent_id, blogpost_id, poster_id, content) VALUES
(:parent_id, :blogpost_id, :poster_id, :content);");
( comment_id, parent_id, blogpost_id, poster_id, content) VALUES
(:comment_id, :parent_id, :blogpost_id, :poster_id, :content);");
// Bind all the parameters
$stmt->bindValue(":comment_id", $uuid, PDO::PARAM_STR);
$stmt->bindValue(":parent_id", $parentId == "NULL"
? NULL : $parentId, PDO::PARAM_STR);
$stmt->bindValue(":blogpost_id", $blogId, PDO::PARAM_STR);
@ -251,7 +265,7 @@ function send_comment($conn, $blogId, $posterId, $content, $parentId) {
// Execute the statement
$stmt->execute();
return null;
return $uuid;
}
/**
@ -378,10 +392,12 @@ if(isset($_POST["submit"])) {
$posterId = isset($_SESSION["current_user"]) ? $_SESSION["current_user"]->user_id : "NULL";
// Try to send the comment
send_comment($conn, $blogId, $posterId, $commentContent, $parentId);
$commentId = send_comment($conn, $blogId, $posterId,
$commentContent, $parentId);
// Redirect to this page with GET
header("Location: http://www.zdenekborovec-dev.cz/blog/article?guid=".$blogId);
header("Location: http://www.zdenekborovec-dev.cz/blog/article?guid=".
$blogId."#comment-".$commentId);
}
// Get the blog id.