When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, May 12, 1999

Creating a Guestbook with ASP
By Joao Vieira


I Know that there's lot of GuestBooks around, but I think that one more won't be too much. And you can see this one working on my site at http://www.joaovieira.com

- continued -

To set up the guestbook, we will need to do the following two things:

1. Create a database with the following structure:

Guestbook
IDAutonumber
DateDate/Time
nameAsText
EmailText
MessageText

2.) Write the code! :)

FILE: Guestbook.asp
This is the only file...... that will make you have an easy guestbook at your site.....

<%
    'the name of the sender
    Dim cxname 

    'the user's email
    Dim cxemail

    'and the guestbook message!
    Dim cxmessage

    'Used for error control...
    'If OK = 1, no errors.
    'If OK = 0, there are errors!
    Dim OK

    Set Conn = Server.CreateObject("ADODB.Connection")

    'I created a DSN named guestbook which points to
    'the database we created with the structure outlined above
    Conn.open "DSN=guestdb;" 

    Set rs = Server.CreateObject("ADODB.Recordset") 

    OK = 1
     
    ' You can see my article about e-mail validation
    ' to better understand this lines bellow
    If Len(Request.form("cxemail")) > 0 and Len(Request.form("cxemail")) < 5 Then
       OK = 0
    Else
       If InStr(1, Request.form("cxemail"), "@", 1) < 2 Then
          OK = 0
       Else
          If InStr(1,Request.form("cxemail"), ".", 1) < 4 Then
             OK = 0
          End If
       End If
    End If


    ' Make sure the name isn't blank...
    If len(Request.form("cxname")) < 1 then 
      OK = 0
    end if   

    'Make sure the message isn't blank
    if len(Request.form("cxmensagem")) <= 8 then 
      OK = 0
    end if

    ' if there is no errors let's put it in the DB
    if OK = 1 then 
       Set rs2 = Server.CreateObject("ADODB.Recordset")
       SQLStmt = "SELECT * FROM guestbook"
       rs2.Open SQLstmt,Conn,1,2
       rs2.AddNew
       rs2("nameAs") = Request.form("cxname")
       ' I get the data thats within
       ' You can see my article 4 Ways To Pass Info between ASP Files

       rs2("email") = Request.form("cxemail")
       rs2("message") = Request.form("cxmessage")
       rs2("date") = month(now) & "/" & day(now) & "/" & year(now)

       rs2.Update
       rs2.Close   
    end if

     SQLStmt = "SELECT * FROM guestbook ORDER BY id DESC"

     rs.Open SQLstmt,Conn,1,2
%>

<html>
<head>
  <title>GuestBook</title>
</head>
<!-- No this is only the code you will have on your page-->

<body 
	BGCOLOR="#385FC6"
	TEXT="#000000"
	LINK="#0000FF"
	ALINK="#FF0000"
	VLINK="#400040"
>
<p>
<img src="images/tguestbook.jpg" width="150" height="38"></p>
<div align="center"><center>
   <table border="0" width="90%">
   <tr>
      <td width="100%">
     <div align="center">
     <center>
     <table border="0" width="90%">
        <%
     'at this time we will put all the messages entered in 
        'the table
     While Not rs.EOF
     %>
           <tr>        
             <td width="100%" bgcolor="#000080">
          <font face="Arial" color="#E7F4D8">
                <strong>Posted in </strong></font>
          <big><font color="#00FF00"><em>
          <%=rs("date")%>
          </em></font></big>
                <font face="Arial" color="#E7F4D8">
          <strong>sent by</strong></font>
          <font color="#00FF00"><em>
          <a href="mailto:<%=rs("email")%>"><big>
                <%=rs("nameAs")%></big></a><br>
                </em></font>

          <div align="center"><center>
                <table border="0" width="70%">
                   <tr>
                     <td width="100%"><p align="center">
                      <font color="#FFFF80" face="Arial">
                      <strong><%=rs("mensagem")%></strong>
                </font>
                     </td>
                   </tr>
                </table>
                </center>
          </div>
             </td>
          </tr>
          <%
            rs.MoveNext 
          wend

          rs.close
       %>
          </table>

          </center></div>
     </td>
      </tr>
    </table>

    </center>
    </div>
    <% 'everything is done ... so lets clean the fields in the form
       cxname=""
       cxemail=""
       cxmessage=""
    %>

    <form name="gbsend" method="POST" action="guestbook.asp">
       <p><font face="Arial"><strong>
       <font color="#E7F4D8">Name :</font>
     </strong></font>
       <input type="text" name="cxname" value="<%=cxname%>" size="20">
     <br><br>
       <font face="Arial" color="#E7F4D8">
       <strong>E-Mail : </strong></font>
       <input type="text" name="cxemail" value="<%=cxemail%>" size="20">
     <br>
       <font face="Arial" color="#E7F4D8"><strong<br> 
       Message :<br></strong></font>
       <textarea rows="3" name="cxmessage"
          value="<%=cxmessage%>" cols="38">
       </textarea>
     </p>
       <p>
       <input type="submit" value="   Sign   " name="B1">
       <input type="reset" value=" Reset " name="B2">
    </form>
</body>
</html>

I think it isn't too hard, but I hope it will help you with your GuestBook.....


João Vieira BIO: (email me!)
- Age : 23
- Student And worker (tryin' to finish university, on the last year of the graduation)
- Workin' now at NetGate Portugal
- Programming little stuff since 1989
- Programming HTML since 1996
- Programming JavaScript since 1997, made some experiences with Java.
- Programming ASPs since the Summer of 1997 (yeap, I didn't had nothing to do. Now I love it.)
- Programming ActiveX since 1998
- Webmaster of www.joaovieira.com


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article