I found a php script on this forum posted by Rodney H. way back in 2005. His script worked well and seemed to be just what I needed to get me started building a guestbook/message board for my website.
However, when I incorporated his script (with a few modifications) into my table based page-layout, it won't work. It is not posting the form data to the "ftboard1.txt" file (which does exist) and therefore obviously not reading the data back into my php page. Obviously, I've done something wrong.
Besides getting this to work:
how and where can I add the date and time to each message submitted?
how can I get the comment section to recognize a paragraph break or line break?
I would greatly appreaciate any help one might give.
<?php
// file
$file = 'ftboard1.txt';
// If file doesn't exist, create it:
if(!file_exists($file)){
$fp = fopen($file, 'w');
$close = fclose($fp);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/shtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Message Board</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style>
a.main:link { color: white; text-decoration: none }
a.main:active { color: white; text-decoration: none }
a.main:visited { color: white; text-decoration: none }
a.main:hover { color: blue; text-decoration: underline }
a.sub:link { color: blue; text-decoration: none }
a.sub:active { color: red; text-decoration: none }
a.sub:visited { color: blue; text-decoration: none }
a.sub:hover { color: #b22222; text-decoration: underline }
input {
border: 1px solid #333;
background-color: #fff;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
color: #000;
padding: 0px;
}
textarea {
border: 1px solid #333;
background-color: #fff;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
color: #000;
padding: 0px;
}
</style>
</head>
<body bgcolor="#dfd8b6">
<?php
// If the form has not been submitted:
if (!isset($_POST['submit']))
{
?>
<a name="#top">
<!--NEW MASTER TABLE-->
<table width="728px" border="1" cellpadding="0" cellspacing="0" align="center"><tr><td>
<div>
<table width="728px" border="0" cellpadding="4px" cellspacing="0">
<tr height="65px"><td width="70px">
<img src="images_plans/QM_insignia.gif" style="float: center" width="65" />
</td><td>
<font face="Eras Bold ITC" size="6" color="3399FF">F Troop</font><br>
<font face="Eras Bold ITC" size="4" color="3399FF">2nd BN, OC BDE, Class 67-13</font>
</td><td width="115px" bgcolor="#ffcc66">
<img src="images_plans/OCS_insignia.jpg" style="float: left; margin-left: 5px" width="55px" />
<img src="images_plans/Regiment_insignia.jpg" style="float: right; margin-right: 5px" height="53px" />
</td></tr>
</table>
</div>
<div>
<table width="728px" border="0" align="left" cellpadding="4px">
<tr>
<td bgcolor="olive"><font face="Verdana, Arial, Helvetica, Sans-serif" size="1"><b><center>
<nobr>[
<a href="home.htm" class="main">Home</a> ] </nobr><nobr>[
<a href="reunion.htm" class="main">Reunion Plans</a> ] </nobr><nobr>[
<a href="contact.htm" class="main">Contact</a> ] </nobr><nobr>[
<a href="roster.htm" class="main">Roster</a> ] </nobr><nobr>[
<a href="experiences.htm" class="main">Experiences</a> ] </nobr><nobr>[
<u>WebLog</u> ]</nobr>
</font></b></center>
</td></tr>
</table>
</div>
<div>
<center><strong><font color="green" size="4" face="Verdana, Arial, Helvetica, sans-serif">F-Troop Message Board<br></font>
<font color="red" size="2" face="Verdana, Arial, Helvetica, sans-serif">
(This Message Board Is Under Construction)</font></strong></center>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="1" valign="top" align="center" bordercolor="#336699" width="728" cellspacing="0" cellpadding="5">
<tr><td width="100%" bgcolor="#336699"><center><font color="white" size="2" face = "Verdana, Arial, Helvetica, sans-serif">
When you submit your comments, they will be posted to the "ftboard1.txt" file.
</font></center>
</td></tr>
<tr><td width="100%">
<table border="0" valign="top" align="center" bgcolor="#336699" width="100%" cellspacing="0" cellpadding="5">
<tr><td align="right" bgcolor="#eeeeee"><font color="black" size="2" face="Verdana, Arial, Helvetica, sans-serif">
First Name: <input name="fname" type="text" id="fname" tabindex="1" size="20" maxlength="30"></td>
<td align="left" bgcolor="#eeeeee"><font color="black" size="2" face="Verdana, Arial, Helvetica, sans-serif">
Last Name: <input type="text" name="lname" tabindex="2" id="lname" size="30"></td>
</tr>
<tr><td colspan="2" bgcolor="#eeeeee"><hr style="width: 100%"; height="50px" color="#336699"></td></tr>
<tr><td width="55%" align="left" valign="top" bgcolor="#eeeeee" class="bordertop"><font color="black" size="2" face="Verdana, Arial, Helvetica, sans-serif">
This Message Board requires you to input your first and last name (above) as well as writing your message (left).<br><br>When completed, click on the
"Submit Message" box to post your message on the board below.</td>
<td width="45%" align="left" bgcolor="#eeeeee"><font color="black" size="2" face="Verdana, Arial, Helvetica, sans-serif">
<textarea name="comments" rows="10" cols="50" tabindex="3" /></textarea></td>
</tr>
<td></td><tr>
<td colspan="2"><center>
<input type="submit" value="Submit Message" />   
<input type="reset" value=" Clear Form " /></center></td>
</td></tr>
</table>
</td></tr>
</table>
</form>
<?php
}
else {
if(empty($_POST['fname']) || empty($_POST['lname']) || empty($_POST['comments']))
{
echo 'Please go back and fill out the form completely.';
exit;
}
else
{
// strip tags
$fname = strip_tags($_POST['fname']);
$lname = strip_tags($_POST['lname']);
$comments = strip_tags($_POST['comments']);
// strip slashes
$fname = stripslashes($_POST['fname']);
$lname = stripslashes($_POST['lname']);
$comments = stripslashes($_POST['comments']);
// file
$oldcontent = file_get_contents($file);
$newcontent = '<b>' . $fname . $lname . '</b><br />' . $comments;
$newcontent .= "\n" . '<br /><br />' . $oldcontent;
$fp = fopen($file, 'w+');
$write = fwrite($fp, $newcontent);
$close = fclose($fp);
// if success
if($write)
{
?>
<meta http-equiv="refresh" content="0">
<?php
}
// else error
else {
echo 'Sorry. Your comments were not saved. Please try again.';
}
}
}
?>
<p></p>
<font color="black" size="3" face = "Verdana, Arial, Helvetica, sans-serif">
Posted Messages:
<p><?php
// Read file, comments:
$fp = fopen($file, 'r+');
while (!feof($fp))
{
$buffer = fgets($fp, 4096);
echo $buffer;
}
$close = fclose($fp);
?></p>
</table>
</body>
</html>