SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Location
    Australia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding comments to a page

    I would like to add the ability to add comments to the bottom of a page. I haven't the faintest idea where to begin, any pointers would be appreciate.

    I have been doing some searches (sitepoint and google) but so far I have not found anything useful.

    Thanks,
    Cassie.

    Background info:I have been developing a website to display images & journal entries from a recent overseas holiday. It is a personal site, however it has served an educational purpose expanding my skillset...important given I am a high school web design teacher.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Location
    Australia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just found

    http://js-kit.com/

  3. #3
    SitePoint Addict Belfast75's Avatar
    Join Date
    Oct 2005
    Location
    London, England
    Posts
    215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A great resource for such questions (apart from SP!) is the W3Schools website;

    http://www.w3schools.com/default.asp

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a moderately simple task once you have a decent understanding for PHP. I'm assuming you don't know any server-side languages.

    If you're not looking to learn the whole language and just want the script to do the task for you; I have to ask, are you planning to use a database (MySQL) to store the comments or a text file?

    While js-kit.com is great, do you want to build the script yourself? If I remember correctly js-kit will leave their "mark" on you site, basically the outside world will know you're using borrowed script.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Location
    Australia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Belfast
    Thanks for the link, I do use http://www.w3schools.com/default.asp as a resource in my classes.

    @Royalty
    Thanks for the response. I do not currently know any server-side scripting (though I have dabbled in php many years ago...it is all long since forgotten). Ditto for using mySQL database. While I have studied databases (and taught them at a high school level), I have never used them professionally.

    I am trying to avoid have to do a big refresh, as I will not use them again. The course that I teach has more than enough on its plate without introducing server-side scripting.

    You asked if I "would use SQL or a flat file.", that answer is that I really have no idea where the appropriate place to start is. If the best way is to learn some php and create a mySQL database then I am confident that I can learn (with lots of help from SP ), however I am looking to avoid that if possible.

    Now I ask you: Is learning php/SQL the best method? Are there good FREE scripts available? etc etc

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No Problem.

    This is probably the most basic comment script:

    Code PHP:
    <style type="text/css">
    	.classBox{
    		width:400px;
    	}
    	.name{
    		background:#106e8f;
    		color:#fff;
    		padding:2px;
    	}
    	.date{
    		font-size:12px;
    		float:right;
    	}
    	.comment{
    		color:#106e8f;
    		border-left:solid 2px; 
    		margin-left:10px;
    		font-size:14px;
    		margin-top:-10px;
    		padding-left:4px;
    	}
    </style>
     
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    	Name:<br/>
    	<input name="name" /><br/>
    	Comment:<br/>
    	<textarea name="comment" rows="8" cols="47" ></textarea>
    	<br/>
    	<input type="submit" name="submit" value="Leave Comment">
    </form>
     
    <?php
    if (isset($_POST['submit'])) {
    	$name = $_POST['name'];
    	$comment = $_POST['comment'];
    	$timeOfComment = date("F d, Y h:i:sa");
    	$storedComments = "comments.txt";
     
    	file_put_contents ($storedComments,'<div class="classBox"><p class="name"><span class="date">'.$timeOfComment.'</span> '.$name.'</p><p class="comment">'.$comment.'</p></div>',FILE_APPEND);
    }
    ?>
     
    <h3>Comments</h3>
    <?php
    $commentLog = 'comments.txt';
    $comments = file_get_contents($commentLog) or die ('There are currently no comments.');
    echo nl2br($comments);
    ?>

    You can use this code virtually anywhere. It only requires you to make a .txt file called "comments.txt" in the directory that you use the script.

    There are multiple things missing from the code. Such as: if you needed to delete a comment you'd need to find it in the text file which will be annoying and tedious, there's also no filter for the comments, you may get spam comments, etc.

    I personally like to use a database when building a commenting application for a web page, I store everything in two self-explanatory functions called "leaveComment()" and "showComments()". If you're looking for a more powerful application then yes, the database route is the one you need to go and I strongly recommend PHP/MySql. The database route is a favorite because aside from it being stronger is more organized. But with more power comes more responsibilities like database security (protection against SQL injection attacks for example). Don't worry though, Sitepoint has tons of help on these subjects.

    To get you started quick I suggest you simply use the code provided above and learn more as you go along.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...the code is also missing a way to protect you from blank comment fields. If you know simple javascript you could use some to see if the submit button was pressed then check to make sure it's not empty. If it is empty then alert the user that they're missing either the name or comment.

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Location
    Australia
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou, you have answered my question perfectly.

    I now know where to start!

    (NB I like the database method better, but then given that I only expect a couple of comments per page, the flatfile might indeed be the better and easier route.)

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome!

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Royalty066,
    Thanks for the comment script posted. I will appreciate it if you could give us a more robust comment script that include the features stated by Harristown (delete option inclusive).
    I look forward to your response.

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you take the database route I was talking about all you would need is an SQL command like:

    Code SQL:
    $commenter=$_POST['commenter'];
    $commentee=$_POST['commentee'];
    $comment=$_POST['comment'];
    $time_of_post= DATE("F d, Y h:i:sa");
     
    $insert = mysql_query("INSERT INTO $table
    	(time_of_comment,commenter,afv,commentee,comment) 
    	VALUES ('$time_of_comment','$commenter','$c_law','$commentee','$comment')");	
    $add_comment = mysql_query($insert);


    And to delete the comment:
    Code SQL:
    $query = mysql_query("DELETE FROM $table WHERE commentID=$commentID");
    $delete_comment=mysql_query($query);


    You need to have knowledge of server side script and SQL.

  12. #12
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As an alternative, you can also use third-party commenting system to add comments feature quickly to your page. You can try Disqus, IntenseDebate, Google's Friend Connect or Facebook Connect.

  13. #13
    SitePoint Zealot
    Join Date
    Apr 2006
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    move your site into wordpress

  14. #14
    SitePoint Wizard silver trophybronze trophy Nadia P's Avatar
    Join Date
    Oct 2004
    Location
    NSW Australia
    Posts
    3,564
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My suggestion is the same as elbot's :-)

    Why reinvent the wheel, particularly as you do not know any serverside programming.

    Wordpress will do exactly what you are after. Many tutorials around how to take your current design and create a wordpress theme.

    Actually something that may be more useful is www.contentseed.com

    It's a commercial product (thought once purchased can be used on an unlimited number of domains) and is a CMS where no database is required. Easy to install and easy to update pages as required.
    I use it my own site to update a news page. However, it can be used to edit any or all pages on a site. Only a matter of adding a few bits of code to a page and you can update the site from any computer via the browser.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •