This article explores adding a comments section to blog or forum posts.
Using my previous project 'OutNoteOrganizer' - which still needs a better name, but all the good ones I could think of were taken - I thought about the design and felt it needed to have a comment section.
Similar to say for example Reddit, though not public, teams or groups of people could want to leave comments on the initial post. The other thing I didn't like about the initial design was that anyone in the associate group could edit and change the content without a good way from the UI to track what was changed.
Now the current version only allows the original post author to edit the post, and other group members with access to the post can leave comments.
How I got here?
In terms of how I visualized the code structure, previously I was hung up thinking that comments should be fields in the post itself. For example comment 1, comment 2, etc. However, while it might be possible to do that, it's complex if you want to have an unlimited number of comments to a post. Adjust the concept a bit and you can think of comments that link to the post but are stored in a different table (in MongoDB a different collection) called 'comments'.
Once you have an HTML form filled in you really only need this code to create a new collection called 'comments'.
$col = $db -> comments; //Point the query at the new collection comments (this will create the collection)
//Collect data from an HTML form
$username = $_POST['author'];
$comment = $_POST['commentcontent'];
$postname = $_POST['postname'];
//Create a PHP array to insert into MongoDB
$insertcomment = ['author' => $username, 'comment' => $comment, 'postname' => $postname];
//Make the insert query / connection - the if / else just catches an error
if ($postcomment = $col->insertOne($insertcomment)){
echo "<h3>Comment added to $postname. </h3>";
}
else { echo "<h3>Error occured while adding that comment. Please contact your IT administrator.</h3>"; }
I also wanted to create a hidden form so that it only appears when a user clicks 'ADD COMMENT'. Initially I was thinking using Javascript and passing an innerHTML call would work. It does for the action, but because the comments need to collect some hidden data (i.e. the post title, the logged in user, etc) in PHP variables, the hidden variables need to exist in the PHP code before the Javascript action takes place.
Explanation: PHP is a server side language meaning all the actions are requests to the server on page load. If the page loaded, but the PHP variables are not present during page load, the script won't know about them. Javascript is a run-time in the web browser itself. This makes it good for these on page actions - like <onclick='dothisfunction()'>. However, passing additional HTML after the page loaded will omit the variables we need.
It turns out that Javascript has styling properties as well. What they do is allow for CSS changes to be applied to specific elements identified by the 'id' tag. What we can do is rather than pass new HTML code to the page - which causes issues with the PHP variables needed - we can more simply change the fields from 'hidden' to 'visible'. This way the PHP variables are known to the script, but the same visual result of handling a pop up can be achieved.
In code - HTML with PHP variables - called using echo in PHP
<button onclick='addcomment()'>ADD COMMENT</button><br><br>
<div id='AddComment' style='visibility: hidden;'>
<form method='post' action='submitcomment.php' id='insertComment'>
<textarea class='giantinput' name='commentcontent' placeholder='Add a comment'></textarea>
<input type='hidden' name='author' value='$loginuser'>
<input type='hidden' name='postname' value='$postname'>
<br><br><input type='submit' form='insertComment' value='Submit'></form>
</div>
In code - JS
function addcomment() {
var commentform = document.getElementById('AddComment');
if (commentform.style.visibility === 'hidden') {
commentform.style.visibility = 'visible';
}
else { commentform.style.visibility = 'hidden'; } }
The above is fairly basic as an example implementation. For better accuracy, it may be the case that the HTML form for the comments needs to collect more than just the post title or name, but also say the post author, the date, or better the object ID to ensure if there are say multiple posts with the same name all the comments do not get pulled into each post.
Just wanted to share a bit of code and discovery using PHP and Javascript hand-in-hand to create a useful layout. Hope it helps.
Full source code and Docker Compose to install is located on GitHub.