Page 1 of 1

Creating a guestbook in PHP

Posted: Tue Oct 26, 2010 6:34 am
by Meca
I am a beginner in PHP and have been given an assignment to create a guestbook in php, where the user can input comments and then be able to view all comments on a seperate page. The code I have come up with so far is as follows:

code for the input form

Code: Select all


<div id="form"><!-- -->
	<form method="get" action="guestbook.php">
	<table>
	<tr><td>Name*</td> <td><input type="text" id="Name" name="Name"></td></tr>
	<tr><td>Website*</td> <td>http://<input type="text" id="Website" name="Website"></td></tr>
	<tr><td>Email*</td><td><input type="text" id="Email" name="Email"></td></tr>
	<tr><td>Comments*</td><td><input type="text" id="Comments" name="Comments"></td></tr>
	<tr><td><input type="submit" value="Submit" /></td>
	</table>
	</form>
	
</div>

code for guestbook.php

Code: Select all

<?php
 
$name = $_GET['Name']; 	
$email = $_GET['Email'];		
$comments = $_GET['Comments'];
$timedate = date("M d, Y (g:i a)", time() + 7200);
 
if ($name == "" or $comments == "") {
	echo "One or more required fields were not completed. Please go back and try again. <br /><br />";
} else {
	$oldinfo = file_get_contents("data.txt");
	$guestbookdata = fopen("data.txt", "w+");
	fwrite($guestbookdata, "<div class=\"post\"><strong>Name:</strong> <a href=\"mailto:{$email}\">{$name}</a> - <em>{$timedate}</em> <br />\n");
	fwrite($guestbookdata, "<strong>Comments:</strong> $comments </div>\n \n");
	fwrite($guestbookdata, "$oldinfo");
	fclose($guestbookdata);
	readfile("data.txt");
	echo ("data.txt");
}
?>
I have also created a text file called data.txt that will store the data created. I'm not sure if I have done this right, I have only had 2 classes in php, so I am a bit lost with this code. We have not used MYSQL so I think the guest book has to be created without the use of it. Any help would be greatly appreciated.

Thanks

Re: Creating a guestbook in PHP

Posted: Tue Oct 26, 2010 7:58 am
by yacahuma

Code: Select all

<?php
    $db = 'guest.txt';
    if (isset($_POST['submitbtn']))
    {
        $line = $_POST['Name'] . '^' . $_POST['Website']  .'^' . $_POST['Email'] .'^' . $_POST['Comments'] . '^' . $timedate = date("M d, Y (g:i a)", time() + 7200) ."\n"; 
        file_put_contents  ($db,$line,FILE_APPEND | LOCK_EX);
    }  
    $guestbook = @file($db);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Untitled</title>
        <link rel="stylesheet" type="text/css" href="my.css">
    </head>
    <body>

        <?
            if (is_array($guestbook))
            {
                foreach ($guestbook as $line_num => $line) {
                    list($name,$website,$email,$comments,$time) = explode('^',$line); 
                    echo htmlspecialchars($comments) . "<br />by " .  htmlspecialchars($name) . 'on ' . $time . '<hr />';;
                }
            }
        ?>
        <p>Sign Me Book</p> 
        <form  action="guestbook.php" method="post">
            <table>
                <tr><td>Name*</td> <td><input type="text" id="Name" name="Name"></td></tr>
                <tr><td>Website*</td> <td>http://<input type="text" id="Website" name="Website"></td></tr>
                <tr><td>Email*</td><td><input type="text" id="Email" name="Email"></td></tr>
                <tr><td>Comments*</td><td><input type="text" id="Comments" name="Comments"></td></tr>
                <tr><td><input name="submitbtn" type="submit" value="Submit" /></td>
            </table>
        </form>

    </body>
</html>