PHP Guestbook Script

Written By: Nick Tiberi

- 08 May 2006 -

Description: In this tutorial we will create a simple but functional guestbook using PHP. All you need is a text editor and a web host that supports PHP. No MySQL is needed.

  1. Create the Form
  2. Handle the Data
  3. View Data Only

Create the Form

Open your text editor and create a new XHTML document. Save the file as "index.php." Now we're going to use some XHTML to create the form:

<div id="form">
	<form method="post" action="guestbook.php">
	<div><label for="Name">Name:</label> <input type="text" id="Name" name="Name" /></div>
	<div><label for="Email">Email:</label> <input type="text" id="Email" name="Email" /></div>
	<div><label for="Comments">Comments:</label> <textarea id="Comments" name="Comments" class="input" rows="3" cols="25"></textarea></div>
	<div><input type="submit" value="Submit" /></div>
	</form>
	
<p>
<a href="viewguestbook.php">View Guestbook</a>
</p>	
</div>

You'll notice that there are references to two other files: "guestbook.php" and "viewguestbook.php." We will worry about those later.

If you view this in your browser, you'll notice that there is no styling to it whatsoever. Add this CSS code between the <head> and </head> tags of your XHTML document to spruce it up a bit:

<style type="text/css">
body {
	background-color: #ffffff;
	font-family: arial, sans-serif;
	font-size: small;
}
 
a {
	color: #1a6307;
	text-decoration: none;
}
	
a:visited {
	color: #1a6307;
}
	
a:hover {
	color: #1a6307;
	text-decoration: underline;
}
	
a:active {
	color: #1a6307;
}
	
#form {
	width: 250px;
	height: auto;
	padding: 5px;
	border: 1px solid #000000;
}
 
form {
	width: 100%;
}
	
label {
	font-size: small;
	font-family: Arial, Sans-Serif;
	color: #1a6307;
	float: left;
	width: 5.5em;
}
	
input, textarea {
	width: 95%;
}
input {
	width: 13em;
	margin: 3px;
}
 
input.button {
	width: 3em;
}
	
textarea {
	width: 20em;
	margin: 3px;
}
</style>

If you don't understand all that, don't worry. Just plug it in and go! Now if you look at the page, you'll see a nice little form – but it doesn't do anything yet.

Next >>