How do I make user comments display?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
guitarscn
Forum Newbie
Posts: 7
Joined: Wed Jan 07, 2009 2:59 am

How do I make user comments display?

Post by guitarscn »

I got as far as displaying a text input box where a user can type in a name and a message (okay, I just stole it from another site, but it seems to work fine). I need the username, the comment they post, and the date it was posted to appear on the same page as the comment box. (The comment box is above the comment area, so when a user submits a comment, it will appear right below it, along with other comments users will submit, separated by a line or something.

Code: Select all

<div class="opt-spe">
<div class="rt">Comments</div>
Add a new comment: </div>
<form name="new_comment" method="post">
<input type="hidden" name="res" value="7029c8f06ef7e12a7d186cfafe2bbd1c" />
<div class="cmtadd"><table cellspacing="0" cellpadding="0" class="cmtadd">
<tr><td>Username: (required)</td><td><input type="text" name="user" maxlength="20" /></td></tr>
<tr><td>Text: (required)</td><td><textarea name="text"></textarea></td></tr>
<tr><td colspan="2" style="text-align: center;"><input type="submit" class="ibut" value="Submit comment" /> <input type="reset" class="ibut" value="Clear" /></td></tr>
</div>
</table>
What do I have to do now? How can I make the submitted comments be saved in a separate file (it doesn't have to be, but I'm assuming that's how it works), and then displayed where I want it to?
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How do I make user comments display?

Post by mattpointblank »

The typical approach is to store the comment in a database and then retrieve them using PHP - this might be a bit complex for a beginner but learning it will be really useful. Google for "php mysql tutorial" and learn the basics of setting up a database, connecting to it, inserting records into it and getting them back again.

A couple of things to bear in mind: as your script stands it'll be susceptible to spambots - maybe try googling for a 'php captcha' to make things harder for them? Secondly, google 'sql injection', which is a hacking method used when you're storing user input in a database. You need to prevent it by sanitising (eg cleaning up) your user input before it gets stored in the database.
guitarscn
Forum Newbie
Posts: 7
Joined: Wed Jan 07, 2009 2:59 am

Re: How do I make user comments display?

Post by guitarscn »

Okay, just in case I might have confused some people about what I need, I'll try to make it clearer. I'm going to use an example from Slashdot:


I post website news on my homepage:


Image





When a user clicks on 'Comments,' it leads to another page that looks just like the homepage, except it only displays that one news article that the person clicked on 'Comments' for:


Image





The format I want for the Comments section below the article is "Posted by [insert user here] on [insert date here]" followed by a break, the person's message, then a break, then a line to separate the comment from the next comment that will be displayed below that.

The Comment input part between the article and the comments would look like this (just not as ugly):


Image


Which is the only thing I already have (the input boxes and Submit button I already have done, but the Submit button doesn't do anything).
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How do I make user comments display?

Post by mattpointblank »

What I described above is exactly how Slashdot (and a million other sites) do it. Google for "php mysql tutorial" and learn about how to save user input in a database and retrieve it again for display using PHP.
guitarscn
Forum Newbie
Posts: 7
Joined: Wed Jan 07, 2009 2:59 am

Re: How do I make user comments display?

Post by guitarscn »

mattpointblank wrote:What I described above is exactly how Slashdot (and a million other sites) do it. Google for "php mysql tutorial" and learn about how to save user input in a database and retrieve it again for display using PHP.
Is there an easier way to do it? Since you don't need to log in or anything
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How do I make user comments display?

Post by mattpointblank »

Well, you could store the comments in a 'flat file' (eg, a .txt file somewhere on your server), but it's basically the same difference, you're storing user input somewhere and retrieving it - you may as well learn a useful skill at the same time. Seriously, don't be put off - learning to do this sort of thing was where my web development career took off.
guitarscn
Forum Newbie
Posts: 7
Joined: Wed Jan 07, 2009 2:59 am

Re: How do I make user comments display?

Post by guitarscn »

I've never considered a career in the tech field (I'm a construction worker) but do you think I can get that kind of job if I learn like PHP and stuff? (Sorry for going a bit off topic)
guitarscn
Forum Newbie
Posts: 7
Joined: Wed Jan 07, 2009 2:59 am

Re: How do I make user comments display?

Post by guitarscn »

I checked this out: http://www.gentlesource.com/comment-script/

But I wasn't sure which snippets of code I needed to copy out to make the information from the form on my website page be put into a MySQL database I just created.
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How do I make user comments display?

Post by mattpointblank »

That one's probably overly complex for what you need - try something like this (follow it step-by-step): http://www.zimmertech.com/tutorials/php ... torial.php

As for careers... who knows! I like putting up shelves, could I become a construction worker?
guitarscn
Forum Newbie
Posts: 7
Joined: Wed Jan 07, 2009 2:59 am

Re: How do I make user comments display?

Post by guitarscn »

mattpointblank wrote:That one's probably overly complex for what you need - try something like this (follow it step-by-step): http://www.zimmertech.com/tutorials/php ... torial.php

As for careers... who knows! I like putting up shelves, could I become a construction worker?
Thanks, but for the last step, it says the page I want the comments to be on has to be php but all my pages are in html. Is there a way to tweak the code so I can put it on my html page? (Because I already have the form and comment area set up the way I want it)
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: How do I make user comments display?

Post by mattpointblank »

.php files are just HTML with php code mixed in. You can't use php code on a .html page though. All you need to do is rename your .html files to be .php and wrap your php code in <?php //code here ?> tags!
Post Reply