PDA

View Full Version : Creating a Guestbook in ASP


admin
06-13-2003, 03:59 PM
ASP Guestbook, of course a simple database can be constructed using following:

*******************
Database name: guests.mdb
Table name: guestbook
Field names:
name
email
url
message
********************

This is my first guestbook. The code is original - and I hope simple and straightforward. The first file is alled "guestbook.asp" and is the form the user sees to enter his/her message to the guestbook. If you examine this file, you will see that this form is sent to another ASP page (or script) called "guestbookThankYou.asp".

I have used a simple javascript script for the button which - when clicked - sends the user to the "guests.asp" page - which displays the guestbook entries. I have tested this script and it works great - www.dodgecountyclassifieds.com/testGuestbook/guestbook.asp - check it out!

* NOTE: Of course, you may need to modify the path for the database, place database in your \db directory.

guestbook.asp

<html>
<head>
<title>ASP Guestbook</title>
</head>
<body>

<font face="ARIAL" size="2" color="BLACK"><b>
Welcome to my guestbook...</b></font></p>
<FORM METHOD="POST" ACTION="guestbookThankYou.asp">
Name: <INPUT NAME="name" size="33"><br>
Email: <INPUT NAME="email" size="33"><br>
URL: <INPUT NAME="url" size="33"><br>
Message: <textarea rows="6" name="message" cols="30">
</textarea><br>
<INPUT TYPE="SUBMIT" VALUE="SEND">
<INPUT TYPE="BUTTON" VALUE="VIEW GUESTBOOK"
onClick="location.href='guests.asp';">
</form>
</body>
</html>

guestbookThankYou.asp

<html>
<head>
</head>
<body>

<p>Thank you for signing my guestbook...</p>
<FORM METHOD="POST" ACTION="guests.asp">
<%
Set cn = Server.CreateObject("ADODB.Connection")
openStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\db\guests.mdb")
cn.Open openStr
SQL = "SELECT * FROM guestbook"
Set record = Server.CreateObject("ADODB.Recordset")
record.Open sql, cn, 2, 2
record.AddNew
record("name") = Request.Form("name")
record("email") = Request.Form("email")
record("url") = Request.Form("url")
record("message") = Request.Form("message")
record("todaysDate") = Now()
record.Update
record.Close
Set record = Nothing
cn.Close
Set cn = Nothing

%>
<INPUT TYPE="BUTTON" VALUE="VIEW GUESTBOOK"
onClick="location.href='guests.asp';">
</form>
</body>
</html>

guests.asp

<html>
<head>
</head>
<body>

<p>Guestlist entries....</p>
<%
openStr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\db\guests.mdb")
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open openStr, UserID, Password
SQL = "SELECT * FROM guestbook ORDER BY 'ID' DESC"
Set record = Server.CreateObject("ADODB.Recordset")
record.Open sql, cn
record.MoveFirst
Do While Not record.EOF
Response.Write"<b>Date</b>: " & record("todaysDate").Value & "<br>"
Response.Write"<b>Name</b>: " & record("name").Value & "<br>"
emailValue=record("email").Value
Response.Write"<b>Email: </b>" & _
"<A HREF='mailto:" & emailValue & "'>" & emailValue & "</A><br>"
urlValue=record("url").Value
Response.Write"<b>URL: </b><A HREF='" & _
urlValue & "' target='blank'>" & urlValue & "</A><br>"
Response.Write"<b>Message</b>: " & _
record("message").Value & "<br>"
Response.Write"<hr width='80%'>"
record.MoveNext
Loop

record.Close
Set record = Nothing
cn.Close
Set cn = Nothing
%>

</body>
</html>

By Chris Perry