• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Week: 3 At a Glance > Day 15—The Guestbook (guestbook.asp)

Day 15—The Guestbook (guestbook.asp)

One of the first projects, the guestbook, required editing the code to properly define the query. The source code for the guestbook is shown here:

<%@LANGUAGE="VBSCRIPT"%>
<!--#include file="../Connections/connChapter15a.asp" --> <%
' *** Insert Record: construct a sql insert statement and execute it
MM_editAction = CStr(Request("URL"))
If (Request.QueryString <> "") Then
  MM_editAction = MM_editAction & "?" & Request.QueryString
End If
If (CStr(Request("MM_insert")) <> "") Then

  MM_tableName = "tblcomment"
  MM_fields =
"name,name,',none,'',email,email,',none,'',message,message,',none,''"
  MM_redirectPage = "guestbook.asp"

  ' create the insert sql statement
  MM_tableValues = ""
  MM_dbValues = ""
  MM_fieldsArray = Split(MM_fields, ",")
  For i = LBound(MM_fieldsArray) To UBound(MM_fieldsArray) Step 5
    FormVal = CStr(Request.Form(MM_fieldsArray(i)))
    Delim = MM_fieldsArray(i+2)
    If (Delim = "none") Then Delim = ""
    AltVal = MM_fieldsArray(i+3)
    If (AltVal = "none") Then AltVal = ""
    EmptyVal = MM_fieldsArray(i+4)
    If (EmptyVal = "none") Then EmptyVal = ""
    If (FormVal = "") Then
      FormVal = EmptyVal
    Else
      If (AltVal <> "") Then
        FormVal = AltVal
      ElseIf (Delim = "'") Then  ' escape quotes
        FormVal = "'" & Replace(FormVal,"'","''") & "'"
      Else
        FormVal = Delim + FormVal + Delim
      End If
    End If
    If (i <> LBound(MM_fieldsArray)) Then
      MM_tableValues = MM_tableValues & ","
      MM_dbValues = MM_dbValues & ","
    End if
    MM_tableValues = MM_tableValues & MM_fieldsArray(i+1)
    MM_dbValues = MM_dbValues & FormVal
  Next
  MM_insertStr = "insert into " & MM_tableName & " (" & MM_tableValues & ")
values (" & MM_dbValues & ")"

  ' finish the sql and execute it
  Set MM_insertCmd = Server.CreateObject("ADODB.Command")
  MM_insertCmd.ActiveConnection = "dsn=ud9;"
  MM_insertCmd.CommandText = MM_insertStr
  MM_insertCmd.Execute

  ' redirect with URL parameters
  If (MM_redirectPage = "") Then
    MM_redirectPage = CStr(Request("URL"))
  End If
  If (InStr(1, MM_redirectPage, "?", vbTextCompare) = 0 And (Request.QueryString
<> "")) Then
    MM_redirectPage = MM_redirectPage & "?" & Request.QueryString
  End If
  Call Response.Redirect(MM_redirectPage)
End If
%>
<%
set rsComment = Server.CreateObject("ADODB.Recordset")
rsComment.ActiveConnection = MM_connChapter15a_STRING
pc="%"
rsComment.Source = "SELECT *  FROM tblcomment WHERE message NOT LIKE '%SUCK%' AND
message NOT LIKE '%<"+pc+">%' ORDER BY messageID DESC"
rsComment.CursorType = 0
rsComment.CursorLocation = 2
rsComment.LockType = 3
rsComment.Open()
rsComment_numRows = 0
%>
<%
Dim Repeat1__numRows
Repeat1__numRows = 10
Dim Repeat1__index
Repeat1__index = 0
rsComment_numRows = rsComment_numRows + Repeat1__numRows
%>
<html>
<head>
<title>Guestbook</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFCC">
<p align="center"><font face="Arial, Helvetica, sans-serif"
size="+1"><b><i>Please
  sign our guestbook!</i></b></font></p>
<form name="frmComment" method="post" action="<%=MM_editAction%>">
  <table width="83%" border="1" cellspacing="0" cellpadding="2" bgcolor="#CCCCFF"
align="center">
    <tr>
      <td width="13%" bgcolor="#FFFFCC"><font face="Arial, Helvetica,
sans-serif">Your
        name:</font></td>
      <td width="87%">
        <input type="text" name="name">
        <font face="Arial, Helvetica, sans-serif">&nbsp;&nbsp;&nbsp;Email
address:</font>
        <input type="text" name="email">
      </td>
    </tr>
    <tr>
      <td width="13%" bgcolor="#FFFFCC"><font face="Arial, Helvetica,
sans-serif">Comments:</font></td>
      <td width="87%">
        <textarea name="message" rows="2" wrap="VIRTUAL" cols="50"></textarea>
      </td>
    </tr>
    <tr>
      <td width="13%" bgcolor="#FFFFCC">&nbsp;</td>
      <td width="87%">
        <div align="center">
          <input type="submit" name="Submit" value="Save Comments">
        </div>
      </td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="true">
</form>
<p>&nbsp;</p>
<p align="center"><b><i><font size="+1">The last 10 people to sign the book
said...</font></i></b></p>
<%
While ((Repeat1__numRows <> 0) AND (NOT rsComment.EOF))
%>
<table width="75%" border="0" cellspacing="0" cellpadding="2" align="center">
  <tr>
    <td>
      <div align="center"><font face="Arial, Helvetica,
sans-serif"><%=(rsComment.Fields.Item("message").Value)%></font></div>
    </td>
  </tr>
  <tr>
    <td>
      <div align="center"><i><font face="Arial, Helvetica,
sans-serif"><%=(rsComment.Fields.Item("name").Value)%></font></i></div>
    </td>
  </tr>
</table>
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
rsComment.MoveNext()
Wend
%>
<p align="center">&nbsp;</p>
</body>
</html>


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint