Grow your CSS skills. Land your dream job.

“Post a comment” box, what is it…?

  • # February 27, 2011 at 10:16 am

    Hi guys

    On many blogs, where you type a response, comment etc, I was wondering, are these boxes just HTML textfields with something like PHP attached to them…?

    If so, how would the php part be coded. Would it start with an if statement that would write assist something in writing it to the database then echo out the HTML again once a post has been posted…?.

    I could look at wpress files but am not sure which one does this job.

    Sorry to be so vague its just I am learning PHP and have never thought about it….

    # February 27, 2011 at 11:18 am

    If you want to look at WordPress files, take a look at the comments.php template file. That is what is responsible for displaying the form as well as the comments.

    # February 27, 2011 at 12:12 pm

    Thanks man

    Just looked at the wp file and now even more confused lol.

    Can anyone make it a bit clearer….???

    # March 21, 2011 at 1:13 pm

    Usually, yes, php or some other type of server-side code is responsible for processing a form and creating some sort of response, depending on what you want that form to do. A comment box usually will require code that cleans up the entry (and makes sure that what’s been entered isn’t going to break your website or erase your database) and possibly validates an email address or website if it’s been entered.

    How it works…. Well, if you look at the HTML for a form (‘View Source’ on this page even), in the opening

    tag there will be two values to note: the ‘method’ and ‘action’. The method usually will be ‘post’ or ‘get’, depending on what the form does. “Get” will create a linkable result, so most of the time people use that for search results or any kind of data retrieval. “Post” is a ‘quieter’ method i guess you could say, and is usually used for login forms, comment boxes, or any time you (as the user) need to give information to the website. Okay. The ‘action’ value tells the browser what to do with the form when you press that submit button. Usually it will name a processing script.

    So, say you’re working with a simple comment box, it might look like this:





    The script in “/forums/vanilla/post/comment” will probably have the overall outline something like this:


    < ?php
    if (isset($_POST)) {
    if (isset($_POST)) {
    // process the comment here
    }
    }
    ?>

    The html form will give information to the php script in the form of $_POST – so for each form input you have, there will be a $_POST array key that matches (or $_GET, if that’s the method you’re using). So if you want to make sure that someone entered text into the box, you can tell php:

    < ?php $var = isset($_POST);  ?>

    … which gives $var a value of ‘false’ if the comment box is empty, and true if it’s not. If you want to have the page print out whatever someone typed in the comment box, then you would type in:

    < ?php echo $_POST; ?>

    … wherever you want the text to appear.

    It gets a lot more complicated than this, because most of the time when using a comment box you’re actually adding information to a database. But this is the basics of how it works. Hope it helps!

    # March 21, 2011 at 1:20 pm

    PS. Yes, if you’re interested, look up some tutorials to get you started. Once you get the hang of writing basic functions and if… then statements, look at the PHP.net Manual for useful functions: the two I used above are isset() and echo (not technically a function, but whatever). The manual is intimidating to start out, but look at the examples for each function, and check the comments and related functions to help understanding how each one works.

    Good luck!

    # March 21, 2011 at 1:27 pm

    You could also use Wufoo if you wanted — it’s basically a plug-and-play form so you don’t have to code everything. Takes a bit of a learning curve, but might get the job done.

    # January 2, 2013 at 1:03 pm

    How to create comment box its showing without refreshing a page?? using PHP without framework… Please help me its urgent coding for me.

    # January 2, 2013 at 1:09 pm

    @ramana0102 – please start your own thread.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘“Post a comment” box, what is it…?’ is closed to new replies.

*May or may not contain any actual "CSS" or "Tricks".