Talk To Me! - Speak Now, Or Forever Hold Your Peace (Page 5 of 8 )
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.Next: Of Trees And Branches >>
More Administration Articles
More By Vikram Vaswani, (c) Melonfire