10.3. Building the Comment Entry Form

You enable users to add new comments to your blog entries by providing them with a form to fill out.

This form needs to contain fields for the commenter's name, email address, and comment. Also, you need to store the blog entry's identifier to associate the comment with the blog.

To display this form, you create a new method called showCommentForm(), which accepts one argument, $blog_id, and outputs a form in HTML.

You can create this method by adding the code in bold to your Comments class in comments.inc.php:

<?php

include_once 'db.inc.php';

class Comments
{
    // Our database connection
    public $db;

    // Upon class instantiation, open a database connection
    public function __construct()
    {
        // Open a database connection and store it
        $this->db = new PDO(DB_INFO, DB_USER, DB_PASS);
    }

// Display a form for users to enter new comments with
    public function showCommentForm($blog_id)
    {
        return <<<FORM
<form action="/simple_blog/inc/update.inc.php"
    method="post" id="comment-form">
    <fieldset>
        <legend>Post a Comment</legend>
        <label>Name
            <input type="text" name="name" maxlength="75" />
        </label>
        <label>Email
            <input type="text" name="email" maxlength="150" />
        </label>
        <label>Comment
            <textarea rows="10" cols="45" name="comment"></textarea>
        </label>
        <input type="hidden" name="blog_id" value="$blog_id" />
        <input type="submit" name="submit" value="Post Comment" />
        <input type="submit" name="submit" value="Cancel" />
    </fieldset>
</form>
FORM;
    }
}

?>

Note that you're using the heredoc syntax; this allows you to return HTML and PHP values easily, without requiring that you use escape quotes.

10.3.1. Modifying index.php to Display the Comment Form

You have a method to display a comment form; your next step is to show it to your users.

You want this form to be visible only on blog entries that are being fully displayed, so you want to work inside the conditional statement that checks whether $fulldisp is set to 1. Then you need to set up a second conditional to make sure you're on the blog and not any other page.

After you're sure the user is in the right place to see the form, you need to include your comment class, instantiate it, and load the form into a variable, which you can call $comment_form.

Finally, you add the comment form to the output, just after the Back to Latest Entries link.

To do this, add the code in bold to index.php, starting at line 78:

// If the full display flag is set, show the entry
if($fulldisp==1)
{

    // Get the URL if one wasn't passed
    $url = (isset($url)) ? $url : $e['url'];

    // Build the admin links
    $admin = adminLinks($page, $url);

    // Format the image if one exists
    $img = formatImage($e['image'], $e['title']);

    if($page=='blog')
    {
        // Load the comment object
        include_once 'inc/comments.inc.php';
        $comments = new Comments();
        $comment_form = $comments->showCommentForm($e['id']);
    }
    else
    {
        $comment_form = NULL;
    }

?>

        <h2> <?php echo $e['title'] ?> </h2>
        <p> <?php echo $img, $e['entry'] ?> </p>
        <p>
            <?php echo $admin['edit'] ?>
            <?php if($page=='blog') echo $admin['delete'] ?>
        </p>
        <?php if($page=='blog'): ?>
        <p class="backlink">
            <a href="./">Back to Latest Entries</a>
        </p>
        <?php echo $comment_form; endif; ?>

<?php

} // End the if statement

Viewing a full entry in a browser reveals your new comment form beneath the entry (see Figure 10-2).

Figure 10.2. Your comment form

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.124.177