<%@ Language=VBScript %> <% Option Explicit %> <HTML> <HEAD> <TITLE>Using the Dictionary object for data collection</TITLE> </HEAD> <BODY> <% '*******************************************************************' ' Using the Dictionary object for data collection ' ' This is an example of how one could use the Dictionary object to ' handle processing of forms or the gathering of data. It is ' usually better to do form validation/processing on the client, but for ' the sake of this demo we'll imagine we are doing some kind of database ' lookup on the server side. ' ' Basically once the Dictionary object "fills up" then specific code is executed. ' You can build a form for use with this page or you can run it by itself directly ' on your server to get the basic idea. ' ' If you are unfamiliar with Dictionary object see the WebDaily article on this site. ' '*******************************************************************' Dim objDict, objErrors, firstname, lastname, phone, email Set objDict = CreateObject("Scripting.Dictionary") Set objErrors = CreateObject("Scripting.Dictionary") ' CheckIn Function: ' checks to see if the strings are valid and if so adds them to the dictionary. ' And if not valid, then add them to the error dictionary. Function CheckIn(name, value, canbenull, length) ' You can go crazy in here with whatever data checks you'd like. If (value <> "") And (canbenull = False) And (Len(value) >= length) Then objDict.Add name, value Else objErrors.Add name, value End If End Function Function HandleArray(Elements) If objDict.Count >= Elements Then HandleArray = True Else HandleArray = False End If End Function ' The Iterate Sub iterates through each key in a dictionary, writing out each one as it goes. Sub Iterate(Dictionary) Dim k, i k = Dictionary.Keys For i = 0 To Dictionary.Count - 1 Response.Write k(i) & "<br>" Next End Sub '*******************************************' ' ******* Get form values ******* ' You can build a form to submit to this page ' These data types are only examples... firstname = Request.Querystring("fname") lastname = Request.Querystring("lname") phone = Request.Querystring("phone") email = Request.Querystring("email") ' If the name value pairs pass the CheckIn Function then ' they are added to the dictionary object Call CheckIn("First Name", firstname, False, 6) Call CheckIn("Last Name", lastname, False, 6) Call CheckIn("Phone Number", phone, False, 6) Call CheckIn("Email Address", email, False, 6) ' ******* Check HandleArray ******* ' When HandleArray is true then we are successful in our data collection. If HandleArray(4) = True Then ' Success! ' You could go to the next page here. ' ADVANCED: You could place the dictionary object in a session variable for use in filling out multi-page forms. ' Be forewarned! This can be very hazardous to server memory. ' Or display a message: Response.Write "<h2>Thanks for filling out our survey.</h2>" objDict.RemoveAll objErrors.RemoveAll Set objDict = Nothing Set objErrors = Nothing Else If objDict.Count > 0 Then Response.Write "<h2>So far you have correctly filled out:</h2>" Iterate(objDict) End If Response.Write "<h3>Please fill out the following:</h3>" Iterate(objErrors) End If %> </BODY> </HTML>