Add comments to static web pages

Want to know what readers think of your article? You can install a complete content management system, spend several weeks moving pages and make the system look like your site has so far, or use a special tool that is easy to install and work on your own.

The comment system is very simple:
- Each page to be commented will have an accompanying XML file that will contain all comments for that page.
- when the article is displayed, the contents of the XML file are dynamically loaded with the Ajax script, and comments are displayed at the bottom of the page.
- the authentication script is additionally used to store logins, passwords, email addresses, etc. in another XML file.

For the first demonstration, the reader can enter a simple comment and save it in the demo-comment.xml file, then this page is loaded, the XML file is loaded with the Ajax script, and the content is analyzed and displayed at the bottom of the page.

Here is the code that scans the XML file using the getElementByTagName () and getElentById () DOM methods.

function processData(doc)
{ 
  var element = doc.getElementsByTagName('login').item(0);
  document.getElementById("login").innerHTML= element.firstChild.data; 
  document.getElementById("comment").innerHTML= 
doc.getElementsByTagName('comment').item(0).firstChild.data; }

Add a comment to the page, demo

In a real application, a login and password are first requested, a comment will be added to the XML file with previous comments, and the contents of this file will be dynamically added to the web page when it is downloaded. Frames are created for each comment.
A full manager for viewing and deleting comments will be available later to shut down the tool.
The page template must include JavaScript.

  1. Enter a login (enter anything) and add a comment to the form.
    Click Add Comment.
  2. The page is loaded, and the login and comment are inserted into the frame at the bottom of the page .

Download Demo

Demo Source Codes:

Comments and multiple users

The next step is to support multiple comments by different users. The XML file for storing comments is in the following format:

<comments>
  <comment login="" post=""/>
  ...
</comments>

When creating a file, the functions of the DOMDocument PHP 5 object are used:

$fname="demo-comment.xml";

$doc = new DOMDocument("1.0", "UTF-8");
$doc->load($fname);
$list=$doc->getElementsByTagName("comments")->item(0);
$comtag= $doc->createElement("comment");
$list->appendChild($comtag);
$comtag->setAttribute("login", $login);
$comtag->setAttribute("post", $comment);

$doc->save($fname);

The JavaScript file extracts the content to load the page using the Ajax function and transfers the XML data to a table.

function checkComments(xcontent)
{
  var dnl = xcontent.getElementsByTagName("comment");
  var arr = new Array();
  for(i=0;i< dnl.length;i++)
  {
    var e = dnl.item(i);
    var login = unescape(e.getAttribute("login"));
    var comment = unescape(e.getAttribute("post"));
    arr[i] = new Array(login, comment);
  }
  return arr;
}

The full source code is archived.

Discussion board

Problem adding comments to web pages

19-12-2011 20:52:12

Adamyan

Hello, I find an insert on my website to allow readers to leave a message or comment. On the Script site, I found what I was looking for, see: [url] https://www.iqlevsha.ru/cms/commentaires.php [/url] it works, but we can't leave a single message while it says they add up. In addition, the displayed message is replaced with the following each time. I searched unsuccessfully for the error. I'm not a programmer, so if someone helps me! There are 4 files to download in zipé format. Thanks.

20-12-2011 20:24:16

webmaster

Hello The article does not actually say this: "For demonstration, a simple comment can be entered by the player and it is saved in the demo-comment.xml file." But tomorrow I will post a demonstration online that adds a few comments. This is a simple example anyway, without user control, which is absolutely necessary, it cannot be used in production.

20-12-2011 21:11:11

Adamyan

Hello, thanks for the response, I'm nervous. I read and re-read and tried everything that was written there, but, as I say, the comments do not remain in the memory in the xhtml file. This is the only thing I need to do to refine my site, the only thing. My server is free and it accepts php because everything works well in php on my site. If you kindly look at where the problem is, in the script or in the code. Thanks very much. Phil

21-12-2011 10:59:57

webmaster

The new script has been posted online.

21-12-2011 16:13:08

Adamyan

Hello, Pleasant, I tried right now, but there is an error that is displayed qand I send: Pars is wrong: syntax error, unexpected T_OBJECT_OPERATOR in/mnt/151/sdb/d/1/monsite/commentib.php on line 25 I found forgetfulness in the xhtml file and fixed it, but it does not work and the error message indicates the file lib.php. Mystery?!!! Phil

21-12-2011 19:36:49

webmaster

I tried hosting in PHP 4.4.9 and got the same error message. It works great under PHP 5. In case 5.3. Try setting up a directory for PHP 5 using .htaccess. This is possible with most residents.

21-12-2011 19:39:37

Adamyan

Hula, I didn't win, I ask myself, so I tell you I'm looking forward to it working.

21-12-2011 21:53:25

Adamyan

I believe it will never work! I turned on the free PHP5 by placing a PHP 1 .htaccess file in it and now there is no error message. However, comments are not saved or displayed. I tried mixes with demos and multiplayer files without success. I thought I found some oblivions and filled them unsuccessfully. I think the problem is with the Free server. Thanks for the help that can help people like me. Phil

22-12-2011 17:09:27

webmaster

I heard that .htaccess must be in UTF-8 format in Free to be accounted for. Call to phpinfo (INFO_GENERAL); allows you to check the version and operation. If nothing is displayed, stopping points such as the "here" echo can also be placed; to find the error.