<%@ Language=VBScript %> <% Response.Buffer = True %> <% Function Search(strFirstName,strLastName) 'search through the text file for the student with strFirstName and strLastname Dim objFile,objFileSystemObject,objFS Dim strDummy1,strDummy2,strDummy3,strFirst,strLast Dim strHomePhone, strWorkPhone, strEMail 'create the FileSystemObject Set objFileSystemObject = CreateObject("Scripting.FileSystemObject") 'create a File object linked to the file specified in the strServerFilePath property Set objFile = objFileSystemObject.GetFile(strServerFilePath) 'open the file as a TextStream object Set objFS = objFile.OpenAsTextStream(1, -2) 'loop through each block of 5 lines of the file - corresponds to a single student Do While Not objFS.AtEndOfStream strFirst = UCASE(objFS.ReadLine) strLast = UCASE(objFS.ReadLine) strHomePhone = (objFS.ReadLine) strWorkPhone = (objFS.ReadLine) strEMail = objFS.ReadLine 'test the name of the student against the data from the file If (UCASE(strFirstName) = strFirst) and (UCASE(strLastName) = strLast) Then 'we have a match so save the data strMode = "Browse" colFormElements.RemoveAll colFormElements.Add "txtFirstName", strFirstName colFormElements.Add "txtLastName", strLastName colFormElements.Add "txtHomePhone", strHomePhone colFormElements.Add "txtWorkPhone", strWorkPhone colFormElements.Add "txtemail", strEMail Search = True objFS.close Exit Function End If Loop Search = False objFS.close End Function Sub SaveStudent 'save the details of the new student to the file Dim objFileSystemObject, objFile, objFS Set objFileSystemObject = CreateObject("Scripting.FileSystemObject") 'test the student record file exists If Not objFileSystemObject.FileExists(strServerFilePath) Then 'no file so create it Set objFS = objFileSystemObject.CreateTextFile(strServerFilePath) Else 'file exists so link to File object Set objFile = objFileSystemObject.GetFile(strServerFilePath) 'open the file as a TextStream Set objFS = objFile.OpenAsTextStream(8, -2) End If 'write the data in the dictionary object to the file, each record is written as 5 lines For Each strItem in colFormElements 'trim off any spaces before writing the line objFS.WriteLine trim(colFormElements.Item(strItem)) Next 'close the file objFS.Close 'clear the TextStream object set objFS = Nothing 'clear the File object set objFile = Nothing 'clear the FileSystemObject object set objFileSystemObject = Nothing End Sub Function FormValidation Dim blnOK 'check the user has completed the required fields of the form blnOK = (Trim(Request.Form("txtFirstName")) = "") OR (Trim(Request.Form("txtLastName")) = "") OR (Trim(Request.Form("txtemail")) = "") 'blnOK will return TRUE if any of the conditions is TRUE because of the OR If blnOK Then 'display a message informing user of the error Response.Write("

Error Information

") Response.Write("

You must enter all the information that has *'s!


") Response.Write("You click the back button on your browser to fix your errors!
") End If 'return the result of the test FormValidation = blnOK End Function 'main part of script 'declare the global variables Dim strMode, strServerFilePath, strDocHeader, item 'declare the dictionary object Dim colFormElements 'initialise the global variables strMode = "Browse" set colFormElements = CreateObject("Scripting.Dictionary") strServerFilePath = Server.Mappath("students.txt") 'loop through all the Form collection members and store the form element values in the dictionary object For Each item in Request.Form colFormElements.Add item, Request.Form(item) Next 'create the HTML head elements strDocHeader= "" & _ "Student Contact Response" 'output the HTML document header Response.Write strDocHeader 'validate the form data, which will output any error messages If FormValidation Then 'terminate the page Response.Write("") 'search for the student specified by the user ElseIf Not Search(Request.Form("txtFirstName"),Request.Form("txtLastName")) Then 'no such student so add them to the file SaveStudent Else 'warn user that the student already exists in the file Response.Write("

Error Information

") Response.Write("

Student already exists in the file!


") Response.Write("You click the back button on your browser to fix your errors!
") Response.Write("") End If %>

Student Contact Response

Thank you for using the Class Registration form!

You entered the following information:

Back to Student Contacts Application.
<% 'flush and close the buffer Response.End %>