Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Programming for Active Server Pages (Web Techniques, Oct 1997)


<%
   '// Create an array of data validation error messages
   Dim dctErrMsg        '// Scripting.Dictionary object of error messages
   Dim ary              '// Array used to display error messages
   Dim ii               '// Counter for displaying error messages

   Set dctErrMsg = CreateObject("Scripting.Dictionary")

   '// Validate form entries. If errors found
   If Request.Form("FName") = "" Then
      dctErrMsg.Add "1", "You must enter your first name"
   End If
   If Request.Form("LName") = "" Then
      dctErrMsg.Add "2", "You must enter your last name"
   End If
   If Request.Form("EMail") = "" Then
      dctErrMsg.Add "3", "You must enter your e-mail address"
   End If

   ary = dctErrMsg.Items      '// Store error messages in an array for looping
   
   If dctErrMsg.Count = 0 Then
      '// User entered valid data. Insert into database.
      
      Dim szValues               '// Single quoted, comma delimited list of 
                                 '// values to insert into DB.
      Dim objConn                '// DB connection object instance
      Dim szInsert               '// SQL for insert query
      Dim qryInsert              '// Insert response. Not really used
      Dim szMailMsg              '// Indicates status of SendMail command
      
      '// Build query
      szValues = "'" & Request.Form("FName") &_
         "', '"& Request.Form("LName") &_
         "', '"& Request.Form("EMail") &"'"

      '// Set up connection and run query
      Set objConn = Server.CreateObject("ADODB.Connection")  
      objConn.Open "CatalogData"    '// Pre-defined ODBC data source
      szInsert = "INSERT INTO GuestBook ( FName, LName, EMail ) VALUES(" &_
         szValues & ")"
      Set qryInsert = objConn.Execute(szInsert)
      objConn.Close

      '// E-mail a thank you confirmation note to guest. Note that this server component,
      '// SMTPsvg.Mail, is not part of ASP per se. It is a third party utility from
      '// Genusa.com. vbCrLf is a VBScript constant the value of which is equivalent to
      '// chr(10) & chr(13) -- it's a carriage return/line feed combination.

      Set objMailer = Server.CreateObject( "SMTPsvg.Mailer" )
      objMailer.FromName = "Welcoming Committee"
      objMailer.FromAddress = "[email protected]"
      objMailer.RemoteHost = "mail.prprpr.com"
      objMailer.AddRecipient Request.Form("FName") &" "& Request.Form("LName"),  &_
         Request.Form("EMail")
      objMailer.Subject = "Thanks for using our guest book"
      objMailer.BodyText = Request.Form("FName") &":" &_
        "We very much appreciate your taking the time to sign up " & vbCrLf &_
        "in our guest book." & vbCrLf &_
        "Your sign name: " & Request.Form("FName") &" "& Request.Form("LName") & vbCrLf &_
        "Your e-mail address: " & Request.Form("EMail")
      If objMailer.SendMail then
         szMailMsg = "Your signup confirmation is in the mail."
      Else
         szMailMsg = "There was an error sending your signup confirmation."
      End If
%>
      <HTML>
      <HEAD>
         <TITLE>Thank You</TITLE>
      </HEAD>
      <BODY BGCOLOR="white">
      <H2>Thank You For Signing Our Guest Book</H2>
      We appreciate your taking the time to fill in our guest book.
      <P><%= szMailMsg %>
      </BODY>
      </HTML>
<% Else %>
      <!-- Build error message page -->
      <HTML>
      <HEAD>
         <TITLE>Data Validation Problem(s)</TITLE>
      </HEAD>
      <BODY BGCOLOR="white">
      <H2>Data Validation Problem(s)</H2>
      Invalid data was entered in the form.
      <HR>
      <UL>
<%
      '// Build error message by iterating through the Scripting.Dictionary
      '// of error messages generated above.
      
      For ii = 0 To dctErrMsg.Count -1 'Iterate the array
        Response.Write( "<LI> " & ary(ii) & "<P>" )
      Next
%>
      </UL>
      <HR>
      <P>Use the Back button on your browser to go back and fix the problem(s).
      </BODY>
      </HTML>
<%
   End If
%>


AL WILLIAMS SIDEBAR

LISTING ONE

<HTML>
<HEAD>
<TITLE>For internal use only</TITLE>
</HEAD>
<BODY>
<% If Session("Debug")=True Then
  Session("Debug")=False
Else
  Session("Debug")=True
End If  %>
Debugging is
<% If Session("Debug")=True Then %>
  On
<% Else %>
  Off
<% End If %>
</BODY>
</HTML>


LISTING TWO

<HTML>
<HEAD>
<TITLE>Bad Array Code</TITLE>
</HEAD>
<BODY>
<% ' Initialize
   Dim ary(10)
   For i = 0 to 10
      ary(i)=i
   Next
   Session("Array")=ary
' Dump it out to prove it is there
   For i=0 to 10 %>
     <%=Session("Array")(i) %><BR>
   <% Next

' Change it
   For i=0 to 10
      Session("Array")(i)=-i
   Next %>
Making Array Negative<BR>

<% ' Dump it again?
   For i=0 to 10 %>
     <%=Session("Array")(i) %><BR>
   <% Next %>

</BODY>
</HTML>




Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.