<% '// 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>

Programming for Active Server Pages (Web Techniques, Oct 1997)
Related Reading
More Insights
INFO-LINK
![]() |
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. |