Recently while working on a free-lance project, a Marriage Website for one of my friend, I happened to create a Guestbook for that website. After which, I decided to write a tutorial on how to create a guestbook in case someone might need it. If you do not know what a guestbook is, in real life a guestbook is basically a diary kept at various places and for various occasions where people can leave their wishes or feedback for any event. In a similar way,Β an online guestbook is a service, which enables you to allow your visitors to leave comments and feedback for any event or any product, which is visible to the public.
Well, developing a Guestbook is not a difficult task. It is pretty simple if you know what you are required to do! (Basically, for any problem, if you know what you are supposed to do, it’s pretty easy!). Let me “pen down” the basic steps involved in development of a guestbook.
- A user is displayed a form, which he or she must fill out.
- A confirmation message is displayed to the user when the comment is saved in the database.
- A user can browse through all the comments posted till now on the website.
To solve this simple problem, we will make use of PHP and as always, I would be using my favorite text editor, Notepad++. If you don’t use Notepad++, I highly advise you to use it. Read more about it here. Also, we will be required to use a database to store the comments and information about the user. We will use a MySQL Database.
Guestbook in PHP
Let’s get started with the process of building our very own guestbook.
Guestbook Form
In this code, we basically redirect the form to a PHP page on our server named “addcomment.php” and then we do the main programming part there.
- Create a new HTML page, and in the body tag of the page, add the following code.
</pre> <form action="addcomment.php" method="post" name="guest">Name:Β Β Β <input type="text" name="name" /> Email: <input type="text" name="email" /> Message: <textarea cols="50" name="message" rows="10"> </textarea> <input type="submit" value="Sign this in the Book" /></form> <pre>
- Now, if you want to add a validation check for the name and email fields, add the following JavaScript code to your head tag.
<script type="text/javascript">// <![CDATA[ function Validate() { var x=document.forms["guest"]["email"].value; var y=document.forms["guest"]["name"].value; if(y==null || y=="") { alert("Please enter your Name! "); return false; } if(x==null || x=="") { alert("Please enter your email address!"); return false; } var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 ||="" dotpos+2="">=x.length) { alert("Not a valid e-mail address"); return false; } else return true; } </atpos+2> // ]]></script>
- Then add the following attribute to the form tag.
onsubmit="return Validate();"
- The complete Form Tag will now look somewhat like this.
</pre> <form action="addcomment.php" method="post" name="guest" onsubmit="return Validate();">
The SQL Part
We now need to create a MySQL table in a database to save our data entered by the user. To do this, we need to run the following query on our MySQL Server. On our server, we had to use phpMyAdmin to create a table in our database.
CREATE TABLE guestbook( id int(5) NOT NULL auto_increment, name varchar(60) NOT NULL default ' ', email varchar(60) NOT NULL default ' ', message text NOT NULL, Primary key(id) );
The PHP Files
Now let’s get creating our PHP files. We need one file which will add the comment to the user and then display a confirmation or error message and one file which will display all the comments stored in our database. First let’s make the addcomment.php file.
- Create a new PHP file and paste the following code in there.
<?php $host="localhost"; //Add your SQL Server host here $user="root"; //SQL Username $pass=""; //SQL Password $dbname="slashcoding"; //SQL Database Name $con=mysqli_connect($host,$user,$pass,$dbname); if (mysqli_connect_errno($con)) { echo "<h1>Failed to connect to MySQL: " . mysqli_connect_error() ."</h1>"; } $name=$_POST['name']; $email=$_POST['email']; $message=$_POST['message']; $sql="INSERT INTO guestbook(name,email,message) VALUES('$name','$email','$message')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } else echo "Values Stored in our Database!"; mysqli_close($con); ?>
- Save this file as addcomment.php in the same folder as the above created HTML file.
- Now, again create a new PHP file, which will display the comments and the names of the people to the public. Name this file “guestbook.php“.
- Add the following code to the file.
<?php $host="localhost"; //Add your SQL Server host here $user="root"; //SQL Username $pass=""; //SQL Password $dbname="slashcoding"; //SQL Database Name $con=mysqli_connect($host,$user,$pass,$dbname); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT name,message FROM guestbook"); while($row = mysqli_fetch_array($result)) { ?> <h3><?php echo $row['name']; ?></h3> <p><?php echo $row['message']; ?></p> <?php } mysqli_close($con); ?>
- Be sure to change the variables (host, username, password, database, and table) in both the above created PHP files.
Download Source Code
Well, that’s it. You are ready to fire it up with some CSS and set it live on your website. This was a quick and easy tutorial for beginners. I hope I enabled you to create a Guestbook for your website. Keep subscribed to Slash Coding for more such updates. You can subscribe via RSS Feeds, Liking our Facebook Page or by Following us on Twitter. It’s your pick! π
Rabbit says
Superb post but I was wanting to know if you could write a litte more on this topic?
I’d be very thankful if you could elaborate a little bit more.
Appreciate it!
Aneesh Bhatnagar says
Hey! Thank you for the feedback. What more would you like me to elaborate upon?
Tamika says
I have ffun with, result in I foynd exactly what I
used to be looking for. You have ended my 4 day long hunt!
God Bless youu man. Have a great day. Bye
Aneesh Bhatnagar says
I’m glad this tutorial helped you! π
Vasiliy says
Well, thatβs it. You are ready to fire it up with some CSS and set it live on your website???
Aneesh Bhatnagar says
Yes! That’s all. Just style your guestbook in the way you want! π
Vasiliy says
Values Stored in our Database!
Vasiliy says
how to make that the message appeared on a site?
Aneesh Bhatnagar says
If you followed the tutorial correctly, that message should be displayed everytime the data is saved in the Database!
EnjeruNyu says
Is it possible to add a function to upload pictures to that?
Aneesh Bhatnagar says
Yes it is possible to upload pictures. That is saved for an upcoming tutorial. Keep subscribed to Slash Coding so that you do not miss any updates from us! π
thando says
How do i make the messages appear on the website. the idea is for people to post messages on my website.
Aneesh Bhatnagar says
Hey Thando,
Under the PHP Files section of this article, have a look at step no. 3 and 4. They show you how to display the contents of your Guestbook to the user.
Regina says
Interesting topic.
Aneesh Bhatnagar says
I’m glad you liked it! π
Jolly says
Hi!
Thanks for this wonderful tutorial. I was wondering if you have similar tutorial for PHP5. Please let me know.
Aneesh Bhatnagar says
Hey Jolly,
You’re welcome. But unfortunately I don’t have a similar tutorial for PHP5.