Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | Adding Comments in PHP
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

Adding Comments in PHP

I will show you how to add comment functionality to websites using PHP programing language.

 
  Author: mat | Version: Dreamweaver CS5 | 15th November 2011 |  
 
 
1.
 

Open a new PHP document and save it as index.php.

 
 
2.
 

Click to choose the Design view.

 
 
3.
 

In the menu click Insert, Form and choose Textarea.

Enter comment_field into ID: field and click OK.

Click Yes if  "Add form tag?" window appears.

 
 
4.
 

Got to the end of the Textarea entry field and press Enter to go into the next line (see picture).

 
 
5.
 

In the menu click Insert, Form and choose Button.

Enter comment into ID: field and click OK.

 
 
6.
 

Now insert (copy/paste) the following code bellow the </form> tag:

<table border="1">
<tr>
<td width="161" bgcolor="#CCCCCC">Comments</td>
</tr>
<?php
$file_name = "comment_base.txt";
//write data
if (isset($_POST['comment_field'])) {
  //save data
  $file = fopen($file_name, 'a') or die("File failed to open");
  $comment = $_POST['comment_field']."\n";
  fwrite($file, $comment);
  fclose($file);
}

//read data
if (is_file($file_name)) {
  $file = fopen($file_name, 'r') or die("File failed to open");
  while (!feof($file)) {
       $line = fgets($file, 4096);
       if (strlen($line)>0) {
            echo "<tr><td>";
            echo htmlspecialchars($line,  ENT_QUOTES, 'UTF-8');
          echo "</td></tr>";
       }
  }
  fclose($file);
}
?>
</table>


 
 
7.
 

Let's see what the code actually does.

In the first part we enter the data

$file_name = "comment_base.txt";
We set the file which the comments will be stored in (comment_base.txt in my case)

if (isset($_POST['comment_field']))
Here we check if the comment was sent to a server. If not the if case does not allow to run the code.

$file = fopen($file_name, 'a') or die("File failed to open");
The fopen function open the file. The 'a' parameter keeps existing data saved.

$comment = $_POST['comment_field']."\n";
In $comment variable the content that was sent to a server gets saved.

fwrite($file, $comment);
The fwrite function writes the $comment value into the file.

fclose($file);
File gets closed.

 
 
8.
 

if (is_file($file_name)) {
The function checks if the file exists.

$file = fopen($file_name, 'r') or die("File failed to open");
Again we open the file, the parameter 'r' means, we will open it in a read only mode. In case of an error we get error message.

while (!feof($file)) {
       $line = fgets($file, 4096);
       if (strlen($line)>0) {
            echo "<tr><td>";
            echo htmlspecialchars($line,  ENT_QUOTES, 'UTF-8');
          echo "</td></tr>";
       }
  }

With the function while we read the data in the chunks of 4096 size usig the fgets function. We keep reading the file data until the eof condition is not fulfilled, meaning until the End Of File.

Function strlen verifies the length of a read line so we don't get empty lines displayed.

To display the comments, we used the echo function. From security reasons we added the
htmlspecialchars function which converts the characters alike > and < into text form and that way comments cannot be published as a HTML code.

fclose($file);
File gets closed.

 
 
9.
 

You can now upload the script to a PHP supporting web server and see how it works.

Click here to download the index.php file created in this lesson.

 
 
 
   
  Please login to post a comment
  Click to open user profile  
alexahills, 30th Aug 2024, 2:11 PM
The escort I booked in Dehradun was amazing. She had a great personality, was very accommodating, and made me feel comfortable from the start. The agency was very professional, and I appreciated their dedication to client satisfaction.https://www.chennaidesires.in/Location/d....
 
 
  Click to open user profile  
alexahills, 30th Aug 2024, 2:12 PM
The escort I booked in Dehradun was amazing. She had a great personality, was very accommodating, and made me feel comfortable from the start. The agency was very professional, and I appreciated their dedication to client satisfaction.https://www.chennaidesires.in/Location/d....
 
 
  Click to open user profile  
alexahills, 30th Aug 2024, 2:12 PM
I’ve used a few escort services in different cities, but Dehradun Escorts Service stands out for its quality and professionalism. The escort I met was stunning, intelligent, and knew how to make the evening truly enjoyable. Highly recommend this service! https://escortsdehradun.co.in/manali-esc....
 
 
  Click to open user profile  
alexahills, 30th Aug 2024, 2:13 PM
From the moment I contacted Dehradun Escorts Service, I knew I was in good hands. The booking process was smooth, and the escort was even more beautiful in person than in her photos. She made my visit to Dehradun unforgettable."https://escortsdehradun.co.in/roorkee-es....
 
 
  Click to open user profile  
timothyferris, 16th Sep 2024, 9:54 AM
This tutorial outlines a basic PHP script to create a simple comment form that saves comments to a text file and displays them https://mapquestrouteplanner.org
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO