Hello guys I'm trying to do something very basic but can't figure it out so far. I have a form and just want to display all the user's comments one after the other in the same page once they have submitted them. It does show a user's comment however when I refresh the page it's gone, I want to keep the comment even once the page has been refreshed and add other comments on top of that. Here's my code:

<form action = "dhegdheg.php" method = "POST"> 
		Name: <br /> <input type = "text" name = "name" /> <br />
		Comment: <br /> <textarea rows = "5" cols = "40" name = "comment"> </textarea> <br />
		<input type = "submit" name = "submit" /> 
		</form>
<?php
		echo "<b id = 'comments'>".$_POST['name']."</b>"."<br />";
		echo $_POST['comment'];
		?>

As you can see it's a pretty basic code and i'd like to keep it that way if possible.

Cheers lads

Recommended Answers

All 2 Replies

You need to use a database to actually store the data for the comments that are being left. Obviously in your code you are only echoing the must recent submission. This will disappear as soon as they submit again or leave the page and no one else would see it. You need a database and set up a table. Most common on PHP hosts would by MySQL although others could be available.

Create a table for your comments with the name 'comments' and the columns "comment_id" (set as the primary key), name, and comment. You could add one for time/date and maybe for e-mail and/or web site address. Totally up to you. But my code will use only those three for simplicities sake.

Before I get to the code is is EXTREMELY important that you 'sanitize' all data sent through form submissions. If you do not it opens up your code and database to different forms of 'hacking', sql injections and other horrible things. Search google for PHP sanitizing and I'm pretty sure that there are forum threads on this site that will help. I know that there are even functions out there that you can pass your sent data to and it will sanitize it for you.

I will not get into how to connect to your database...you can look that up as well but this code will work once you have a valid connection to your database.

<php
$action = $_POST['action'];

# This line will tell if the form has been submitted and that it should
# save the comment to the database which it will do before the page is 
# rendered so once the form is submitted the visitors comment will appear 
# with the rest of the comments.

if ($action == "save_comment") (save_comment();}

$comment_row = mysql_query("SELECT * FROM comments");
while($a_comment = mysql_fetch_array( $comment_row )) {
$comment_id = $a_comment['comment_id'];
$name = $a_comment['name'];
$comment = $a_comment['comment'];

echo "
<table>
 <tr>
  <td>$name</td>
 </tr>
 <tr>
  <td>$comment</td>
 </tr>
</table>
<br />
";
}
?>

<form action=comments.php method=POST>
<input type=hidden name=action value="save_comment">
<table>
 <tr>
  <td><input type=text name="name"></td>
  <td><h2>Name</h2></td>
 </tr>
 <tr>
  <td><textarea name="comment" cols=25 rows=8></textarea></td>
  <td>Comment</td>
 </tr>
 <tr>
  <td colspan=2><input type=submit value="Post Comment"></td>
 </tr>
</table>
</form>

<?php
function save_comment() {
$name = $_POST['name'];
$comment = $_POST['comment'];

mysql_query("INSERT INTO comments 
(name, comment) VALUES('$name', '$comment' ) ") 
or die(mysql_error());
}
?>

Now several things to note: The code will put the comment submission form under all of the comments that were left. You can copy/paste it above the database query that gets all of the comments rows and prints out the comment tables which will put the form above all of the comments. But if you do this make sure that you close the PHP tag "?>" the line above where you paste the form HTML and on the line after it open php back up "<?php".

You may notice that we are not passing a value for "comment_id" but you do not have to. If you set the column in the database table to auto_increment it will do just that...it will increase the number in that column by one for each comment left. This is necessary in case you want to reference a particular comment for editing/deletion which I won't get into right now but it's important that it is there.

You may also want to look into "pagination" which will create 'pages' of comments where you can specify how many comments should be shown on each page. This is a good thing if you end up with hundreds of comments.

Good luck.

Rockstar first of all thanks for the help, now I've used your code and got a few errors so I created a connection to my server but now I get the following

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/"databasename"/public_html/dhegdheg.php on line 21

"databasename" is where the actual name of my database is but I changed it for security reasons

This is what I added to your code

$host = "mysql13.000webhost.com";
$username = "************";
$password = "************";
$db_name = "************";

mysql_connect("$host", "$username", "$password") or die("Could not connect to server");
mysql_select_db("$db_name") or die("Could not connect to database");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.