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:

  1. 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>[&nbsp; <a href="home.htm" class="main">Home</a>&nbsp;] </nobr><nobr>[&nbsp; <a href="reunion.htm" class="main">Reunion Plans</a>&nbsp;] </nobr><nobr>[&nbsp; <a href="contact.htm" class="main">Contact</a> &nbsp;] </nobr><nobr>[&nbsp; <a href="roster.htm" class="main">Roster</a> &nbsp;] </nobr><nobr>[&nbsp; <a href="experiences.htm" class="main">Experiences</a>&nbsp;] </nobr><nobr>[&nbsp; <u>WebLog</u>&nbsp;]</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" />&nbsp &nbsp <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>

    Look at somewhere here.

    // 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); 
    

    This is the place where the content is write to the textfile to store the content (or comments, etc). Take a look at date() function, you can store date and time format different. I'll use a default one. Add in this function at this particular line.

    $newcontent = '<b>' . $fname . $lname . '</b><br />' . $comments . '<br/>' . date("F j, Y, g:i a");
    

    I'm not sure if I understand your question 2. But if this is what you want, each line break is done using \n.

      Thanks Joseph07. I did finally get the script to work and was able to get the date and time to post with each message.

      I'm still fighting the paragraph thing and haven't gotten it to work yet. Maybe I didn't make myself clear on my first message. When I type more than one paragraph into my "textarea" form it looks fine. However, when the message is submitted to the text file, it ends up there as if it is all one paragraph. Obviously, when it is then copied back to the php file, it comes back as all one paragraph.

      If I type in a <br>xxx</br> or <p>xxx</p> then it comes out fine. I just don't want my users to have to do this.

        Thanks Joseph07. I did finally get the script to work and was able to get the date and time to post with each message.

        I'm still fighting the paragraph thing and haven't gotten it to work yet. Maybe I didn't make myself clear on my first message. When I type more than one paragraph into my "textarea" form it looks fine. (I'm hitting the "Enter" key once for a line break and twice for a paragraph break.) However, when the message is submitted to the text file, it ends up there as if it is all one paragraph. Obviously, when it is then copied back to the php file, it comes back as all one paragraph.

        If I type in a <br>xxx</br> or <p>xxx</p> then it comes out fine. I just don't want my users to have to do this.

          I figured it out and feel like a stupid Newbe!

          The nl2br() did the trick.

          Thanks again for the help.

            Well, that's good. Learn also you go along.

              I spoke too soon! Those dang forward slashes are about to do me in.

              If my textarea message includes an apostrophe, a single or a double quote then these guys get posted to my text file with a preceding forward slash.

              As you recall, my code now includes the following:

              $comments = strip_tags($_POST['comments']);
              $comments = stripslashes($_POST['comments']);
              $comments = nl2br($_POST['comments']);
              

              The nl2br() function gives me the desired <br />.
              The stripslashes() function certainly removes undesired slashes.
              Having both gives me back the undesired slashes.

              What's a guy suppose to do?

                In the code you provided, neither strip_tags() nor stripslashes() are being used; after each line, you're overwriting the value of the $comments variable with a new value. Thus, the only thing you do to incoming data is apply the nl2br() function since the results of the other two functions are lost.

                If backslashes are automatically being prepended to single quotes, however, the real solution you need to look into is disabling the magic quotes "feature". If you wanted to make your application portable, however, you could check if magic_quotes_gpc is enabled and reverse its effects if so:

                if(get_magic_quotes_gpc())
                    $_POST = array_map('stripslashes', $_POST);
                // Note that this would only clean up POST'ed elements, and that
                // it wouldn't recursively clean any arrays POST'ed.

                  Thanks bradgrafelman for your quick reply.

                  I removed both the strip_tags() and stripslashes() functions and just left the nl2br() function. The results are the same. I still get all the undesired forward slashes and they do prepend all single and double quotes AFTER the message is sent to the text file.

                  My hosting server shows that the magic_quotes_gpc is "On"

                  I'll try your "if" statement and let you know what happens.

                    BigJohn wrote:

                    I removed both the strip_tags() and stripslashes() functions and just left the nl2br() function. The results are the same.

                    Which is what I was saying; the way your code was written, neither of these two functions were doing anything. Thus, removing them does nothing to the data since they weren't being used in the first place.

                    BigJohn wrote:

                    forward slashes

                    Forward slashes? Are you sure? You're saying that instead of don't you're seeing don/'t ? Or are you seeing don\'t with a backslash (or backward slash) in front of the quote?

                    BigJohn wrote:

                    My hosting server shows that the magic_quotes_gpc is "On"

                    You can disable this for your entire site, assuming that PHP was installed as an Apache module, by placing the following code in a .htaccess file in the root of your site:

                    php_flag magic_quotes_gpc off

                      Sorry! I am getting backslashes as in the following:

                      Let\'s get ready.
                      Say \"Oh My Golly\"
                      And: \'Try this.\'

                      I put the disable php command into my .htaccess file on the server. However, when I tried to open my website, I now get an Internal Server Error which says:

                      "The server encountered an internal error or misconfiguration and was unable to complete your request."

                      Now what?

                        Now it looks like you can't disable magic_quotes_gpc for your entire site, so you can either a) use the if() statement like I illustrated (you'll have to modify it if you submit arrays), or b) find a competent host and move.

                          I was able to remove your suggested code from the .htaccess file and login to my website.

                          I tried your if() statement and unfortuantely it made no change (I am not using any arrays).

                          I would assume GoDaddy is pretty competent, but then what do I know.

                          I haven't written any if() statements myself, but wonder if one could write one to look through the comment string looking for any \' and \" and if it finds one to remove the bloody backslash? Just a thought.

                            I can't seem to let this dead horse alone!

                            After this runs:

                            $comments = nl2br($_POST['comments']);
                            

                            Can a script such as the following solve the backslash problem? The following was just a test I ran (but I'm getting an error in line 6) ??:

                             // TEST STRING REPLACE
                            // I WANT TO REMOVE ANY \ WHICH PRECEEDS A SINGE OR DOUBLE QUOTE ONLY
                            
                            <?php
                            $msg = "Let\'s go to school. I would like to sing \"HAPPY NEW YEAR\". I do not want to remove this backslash. <br \>";
                            $bad = array("\\"", "\\'");
                            $good = array(""", "'");
                               echo str_replace($bad, $good, $msg);
                            ?>
                            

                            One would think this str_replace() would resolve the problem.

                              <?php
                              $message = $_POST['message'];
                              
                              $message = str_replace("\" , "" , $message );
                              
                              echo $message;
                              ?>
                                Write a Reply...