What are we doing here?

This blog includes a series of videos and references to help new users or enthusiasts better understand how to use open source and free technology tools. The quick links includes more information for accessing many of the tools covered along with other references to learn more for taking advantage of these tools.

Click HERE to see the full list of topics covered!
Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Adding Comments to Posts - MongoDB and PHP

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'.


Again with MongoDB, there is no need to 'prime' or 'setup' the table. Just from the code tell the DB what fields you want and it will accept the values as a BSON object. 

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.

 

A quick JS interlude

 


This video is a quick - very quick by my standards - video on a short script and interacting with a website to provide real-time updates to users. The script essentially just pulls in variables and outputs the product (multiplying price * amount) so that a user's total order price changes as they select a different amount. 

Most of what I know of JavaScript - which is honestly not a lot - is from the excellent tutorials at W3schools. I've not spent as much time with other elements of how JavaScript interacts with databases and helps create more dynamic websites, but I am still learning as I go. 

A couple more sites and references which would be good for future reading and understanding are below. Several are beyond what I've spent time with having focused mostly on PHP and Linux the past couple of years, but I know that they are powerful tools and hope the references can give those interested some more direction for future areas to study to improve their web development skills.

Angular - Typescript- and JavaScript-based (AngularJS) web development platform, led by Google Angular team.

JQuery - more advanced functionality using the JavaScript framework, also includes some potential for DB interaction, though less a focus than PHP/mysqli.

w3schools Web Development Roadmap - a great overview for all the various building blocks that go into web development. 

There are a ton of things that go into building a strong website. This is exactly why so many business chose to pay to have things hosted on Amazon or setup a site with Shopify or Wix. I personally don't like default templates and structure for any site I want to create as often the templates are extremely complex or only partially editable at the HTML/JavaScript/PHP level. That said, things like security - SSL/TLS encryption, secure payment methods, and servers for hosting, and a good CDN for helping copy and deliver content the world-round are costly. Most platforms designed to host a site for you take care of a lot of that back end and any creator would need to weigh said costs with the flexibility of developing a site completely from scratch. 

Hopefully these tutorials can help those looking to just gain a basic understanding of what goes into web design.


Introduction to Programming: Python, Java and Javascript

This is an overview of writing a basic converter program in 3 different languages, Python, Java, and JavaScript. I personally have used this as an exercise to learn new languages particularly the process of getting variables, manipulating them, and returning an output. These concepts will help with almost any program that you want to make in the future.


The video took a little longer to talk through than I anticipated, but 15 mins per language is hopefully acceptable. Again just something I hope can help people get started.

Source code:
Links to more information:
A few books and other sites that are helpful - Intro to Python, Learn Java in 8 hours, W3 schools, and more. Here are a few links. There are also countless tips and tricks just by Googling and searching through https://stackoverflow.com/.
Here as well is a quick video about how to setup the OpenJDK package in Linux. Alternatively, Windows, MacOS, and RedHat Distros can install from Oracle directly or at the above link.