Home arrow Site Administration arrow Page 5 - Talk To Me!

Speak Now, Or Forever Hold Your Peace - Administration

You've seen how many Web sites (including this one) allow you topost comments and opinions on the material they publish. If you've everwished you had this capability on your Web site, or are simply curious asto how this is accomplished, read on.

  1. Talk To Me!
  2. The Job
  3. Building The Foundation
  4. Mole In A Hole
  5. Speak Now, Or Forever Hold Your Peace
  6. Of Trees And Branches
  7. Closing The Loop
  8. The Last Word
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 1
March 12, 2001

print this article


Next, I'm going to construct a form which will ask for some basic user information, and allow the user to enter a comment on the article. Once that form is submitted, a PHP script will process the information entered into it and store the result in the database via an INSERT operation.

For purposes of convenience, both the initial form and the result page have been embedded in the same PHP script, "post.php", with the $submit variable used to decide which page is displayed. Here's the initial form.

<?php // post.php if($submit) { // form has been submitted // form processing code } else { // display initial form ?> <form action="<? echo $PHP_SELF; ?>" method="post"> <input type="hidden" name="replytopost" value="<?php echo $replytopost; ?>"> <input type="hidden" name="section" value="<?php echo $section; ?>"> <input type="hidden" name="article" value="<?php echo $article; ?>"> <table border="0" cellspacing="5" cellpadding="0"> <tr> <td colspan=2> <font face="Verdana, Arial" size="2" color="Black"><b>Your comment:</b></font><p> </td> </tr> <tr> <td><font face="Verdana, Arial" size="2" color="Black">Name</font></td> <td><input type="text" name="username" size="25"></td> </tr> <tr> <td><font face="Verdana, Arial" size="2" color="Black">Email address</font></td> <td><input type="text" name="email" size="25"></td> </tr> <tr> <td><font face="Verdana, Arial" size="2" color="Black">Subject</font></td> <td><input type="text" name="subject" size="25"></td> </tr> <tr> <td><font face="Verdana, Arial" size="2" color="Black">Comment</font></td> <td><textarea name="post" rows="5"></textarea></td> </tr> <tr> <td colspan=2 align=center><input type="submit" name="submit" value="Add Comment"></td> </tr> </table> </form>

Here's what it looks like:

Once the form is submitted, the same script is called upon to process the data.

<?php // post.php if($submit) { // form has been submitted // process form data include("config.php"); $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); // indicates new comment, not reply if (!$replytopost) { $replytopost = 0; } // correction for empty subject // all form fields should be checked before executing the query if (!$subject) { $subject = "No subject"; } // correction for empty username if (!$username) { $username = "Anonymous"; } $query = "INSERT INTO $table (section, article, subject, post, username, email, replytopost, timestamp) VALUES ('$section', '$article', '$subject', '$post', '$username', '$email', '$replytopost', NOW())"; $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query . " . mysql_error()); ?> <font face="Verdana, Arial" size="2" color="Black"> Your comment has been posted. Click to <a href="list.php?section=<? echo $section; ?>&article=<? echo $article; ?>">read more comments</a>. <?php } else { // display initial form } ?>

This script simply checks the form variables, corrects empty ones, and then INSERTs the data into the database. If this is a "new" comment (as opposed to a "reply"), the $replytopost variable is set to 0.

In case you're wondering what the include() is all about at the top of the script, the file "config.php" simply stores some basic parameters required to connect to the database. Here it is.

<? // database parameters // alter this as per your configuration $database="db39492"; $table = "comments"; $user = "root"; $pass = "gsfd3k6"; $hostname = "localhost"; ?>

Once the data has been stored in the database, a success code is returned, and a link offered to read other comments that may have been posted. Let's take a look at that next.

This article copyright Melonfire 2001. All rights reserved.

>>> More Site Administration Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates


Dev Shed Tutorial Topics: