Creating a guestbook in PHP
Posted: Tue Oct 26, 2010 6:34 am
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 for guestbook.php
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
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: 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");
}
?>
Thanks