Here is a ready to run, one page script which generates a Guest Book. This script is totally stand alone. All you need to do is give it a name, add it to your web site and link to the file. It runs itself from there. And best of all, there's no required database!
This script stores all of its data in a simple text file, called GuestBook.txt. The script creates this file using the FileSystemObject when the first entry is made. After that, all other entries are appended. Viewing the entries is made possible by reading the text file lines and writing back to the browser.
The script structure represents four parts.
1) The initial page setup which collects input and instruct the script on which action to take next. 2) Page 1 which presents the data entry form to the user whith an option buttion to view the guest book. 3) Page 2 which thanks the user for the entry thereby giving confirmation as well an option button to view the new entry. 4) Page 3 which shows all entries in the book and an option button to return to the entry form.
So, with that introduction, here's the script and how it's constructed.
Part 1.
Some standard code is introduced at the beginning to prevent cacheing of the page.
Some contants are created for use with the FileSystemObject, and for retrieving the name given to the script file as well as the path name to be used for the text file.
Since we want to minimize the bad entries into the book, a little bit of server-side validation is performed, checking for residual default values and blank entries. In the current setup the email adress is not required.
If (request("key")=1 and trim((request("LastName"))="" or request("LastName")="Required" _ or trim(request("FirstName"))="" or request("FirstName")="Required")) then response.redirect strScriptName & "?key=0" end if
If trim(request("Email"))="Optional" or trim(request("Email"))="" then strEmail = "No Address Provided" else strEmail = trim(request("Email")) End if
%>
And the final portion of Part 1, is the creation of the page which includes two client-side scripts for buttons to be selected by the user. The scripts pass flags with the URL address to be used by the script after it calls itself.
<html> <head> <title>Guest Book</title> </head>
Part 2.
This is the first page the user will see when the sript runs. It is a form that collects the user's name and email address. It uses the 'get' method for passing the data.
<body bgcolor="#FFFFFF">
<% '//PAGE 1********************************* If Request("Key") = "" or Request("Key") = "0"then %> <h1>Guest Book</h1> <hr color="Silver">
<FONT color="red" ><p><h3>Welcome !</h3></p></FONT> Thank you for visiting this website.<br> Please take a moment to sign the Guest Book.</p>
This part of the script produces the second page. Using the FileSystemObject the GuestBook text file is created if one does not exist,and adds the entries tot the list. If it does exist, it only appends the entries. This page then thanks the user for the entry and serves as a confirmation that the entry has been added to the book. For visual confirmation, the user is given a button to view the entry listing.
'//PAGE 2**********************************
If Request("Key")=1 then
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.fileExists(strphysicalpath) then Set objfile = objfso.OpenTextFile(strphysicalpath,8,true) else Set objfile = objfso.CreateTextFile(strphysicalpath) end if
This part of the script produces the third page. Again, using the FileSystemObject, the GuestBook.txt file is read and the data is written back to the screen. If the user has come here from the first page , without making an entry, a button is provided to take them back to the first page, encouraging an entry be made.
<%'//PAGE 3*********************************%>
<%if Request("Key")=2 then%>
<h1>Guest Book List</h1> <hr color="Silver"><br> <B>Registered Guests at this site.</B><br>
<%
Set objfso = CreateObject("Scripting.FileSystemObject") Set objfile = objfso.GetFile(strphysicalpath)
Dim objFileTextStream set objFileTextStream = objFile.OpenAsTextStream(ForReading, TristateUseDefault)
do while not objFileTextStream.AtEndOfStream fname = objFileTextStream.ReadLine lname = objFileTextStream.ReadLine email = objFileTextStream.ReadLine