How To Easily Add Comments to Your Website With PHP and XML

You will need three files

  • Comments.php – PHP class that will handle comments
  • postcomments.php – PHP script that will add new comments
  • page.php – where your comments and comment form will appear

Comments Handler Class

This class will have a private variable and three functions.

var $commentsFolder='comments';

public function post($pageID,$comment,$author);

public function show($pageID);
private function get($pageID);

Let’s go over what each of the functions will do.

  • post – add new comments
  • show – display comments
  • get – return comments variable to be used by show()

Handling New Comments

We will be storing comments in textfiles, each of this files will represent an article, page or any type of content in your site, all you need is to give a unique id to each piece of content.

Each comment in each file will be put inside its own <comment> tag and each tag will have an attribute for the author.


<comment author="someones name">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce odio leo, vestibulum non hendrerit varius, 
condimentum nec turpis. Suspendisse id lectus vitae nulla lobortis tristique.
Donec semper varius lorem at laoreet. 
Maecenas neque sapien, blandit nec pretium sit amet, adipiscing ac arcu. Mauris congue turpis ut tortor dignissim
 Etiam rhoncus dapibus dolor, non pretium risus vehicula non.

This is how our post function needs to look like.

class Comments
var $commentsFolder='comments';

	public function post($pageID,$comment,$author)
		$newComment="<comment author='{$author}'>{$comment}</comment>";
		echo "
		<b>Author:</b>{$author}<br />

Getting and Formatting Comments

Our comment files are not XML files because they are missing the XML header, they also need a main tag around all the comments. We can’t add this every time a new comment is posted, but we always do this later, this is purpose of the get() function, which will be called inside the show() function.

The get function:

	private function get($pageID)

		$xml='<?xml version="1.0" encoding="UTF-8"?>
			return $xml;
			return false;

Now that get has created an XML string we can use show() to format and display the comments.
Learn how to read XML with PHP

	public function show($pageID)
		$comments=$this->get($pageID); // get() turns the comment files into an XML string.

			foreach($xml->comment as $comment)
				echo "<p>
				<b>Author: </b>{$comment['author']}<br />
			echo 'No comments to show';

The Comment Form and Showing Comments:

Now that our comment class is ready we can build a comment form for articles on website. We will identify what comments we need to load via $_GET, but you can get values for the comment function anyway you want.

Our comment form will be simple but it will be "ajaxified" with JQuery.
Learn AJAX with JQuery

// include comments class
require_once "Comments.php";
// create comments object
$comments=new Comments();

<div id="content">


<label>Leave a comment:</label><br />
<label>your name</label><br />
<input type="text" id="author" /><br />
<textarea id="comment" rows="10" cols="50">

<input type="hidden" id="pageID" value="<?php echo $pageID;?>" />
<button id="post-comment">Post</button>

<div id="comments-box">
// show comments

<script type="text/javascript" src=""></script>
<script type="text/javascript">
// set variables
var comment;
var author;
var pageID=$("#pageID").val();
var postUrl='postcomment.php';

		// get comment
		// send values to script
			// append new comment to comment textbox
			// clear comment textarea

Receiving the New Comment:

We are one step away from finishing this script, and this last script is short! All we need to do is get the POST data from our comments form and pass it to the comments class’ function post()

require_once "Comments.php";


$comment=new Comments();