A Simple
GuestBook or Feedback Page Requiring No Database Implementation
Ken Alabi
As a web designer, I have been faced with the need for a simple and
quick testimonial or guest book application on a basic website with no
database backing. At every ocassion, I shirked at the prospect of
requiring the website have a database (with or without an expensive DSN)
simply for this purpose. I
developed this simple text file powered database in ASP to serve the many
times I will again be faced with this situation and it has made my work
look good to many clients since.
The guestbook application is in the form of a single ASP script file.
The application receives input from site visitors and stores the input in
a simple "tag" delimited text file. The text file implementation is such
that it is XML compatible. The current implementation displays the log of
feedbacks entered by site visitors but could also be implemented to not
display the entries. The solution could be adapted to email the guestbook
entries to a specified address.
How It Works
There are four parts to the ASP page
(1) Entry of information or feedback by site visitors
(2) Validation of the entered information
(3) Appending the information to a text file containing all entries
(4) Displaying all feedback that resides in the text file
Entry of Information
This is achieved by a simple html form containing a textbox for the
message and an input box. A salient feature of the form shown below is the
hidden form object named "submitted" which will be used in
determining the action taken by the page when accessed. Notice that the
action property of the form returns to the same file as represented by the
ASP string (Request.ServerVariables("SCRIPT_NAME"))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
<form name="feedform" method="POST"
action = "<%=Request.ServerVariables("SCRIPT_NAME")%>"
OnSubmit="return Validate()">
<input type="hidden" name="submitted" value>
<table border="0" cellspacing="0" width="100%">
<tr><td width="30%"><b>Enter your comments</b></td>
<td width="70%"><textarea Name="feedbody" COLS="40" ROWS="4"></textarea></td></tr>
<tr><td width="30%"><b>Enter your Name</b></td>
<td width="70%"><input maxLength="50" Name="feedname" VALUE SIZE="45"></td>
</tr>
<tr><td width="30%"></td>
<td width="70%"><input type="submit" value=" Submit "></td></tr>
</table>
</form> |
Validation
There are two validation features in the javascript validation subroutine
"Validate()". First is to check that an input was entered before
the form is submitted to the server. The advantage of this procedure is
that validation is completed on the client computer and requests are not
sent to the server (consuming central resources) until they are
processable.
The second function of the validation subroutine is to set the
submitted form object to true (Line 3). When the page is accessed the
second time by the server, the submitted flag determines that the page is
to be processed for input. This allows the entire application to be
completed with one single ASP file!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
<script language="JavaScript">
function Validate() {
document.feedform.submitted.value=true;
var string1 = document.feedform.feedbody.value;
if (string1 == ""){alert("You have not entered a message"); return false;}
if (string1.length >4000){alert("Your message is too long. This server accepts messages totalling only 4000
characters"); return false;}
var string1 = document.feedform.feedname.value;
if (string1 == ""){alert("You have not entered your name or signature");return false;}
return true;
}
</script>
|
Processing
The processing procedure performs additional activities to ensure that
only one message is posted by the website visitor in one session as well
as to strip the entry of any html elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 |
if (lcase(Request.Form("submitted")) = "true" )Then
if (IsNull(Session("feedbacked"))) then
Session("feedbacked") = False
end if
if (Session("feedbacked")=True) then
errStr = "You are allowed only one message per session.</font><br>"
else
Session("feedbacked")=True
smessage = Request.Form("feedbody")
smessage = Trim(smessage)
smessage = Replace(smessage,"<","")
smessage = Replace(smessage,">","")
sname = Request.Form("feedname")
sname = Trim(sname)
sname = Replace(sname,"<","")
sname =
Replace(sname,">","")
sdate = Now()
smessage = smessage & "<br><b><font color=blue>- " & sname
smessage = smessage & " (" & sdate &
")</font></b>"
sfile = "_feedback.txt"
whichFN=server.mappath(sfile)
if (whichfN <> "") and (Not
IsNull(whichFN)) then
Set fstemp = server.CreateObject("Scripting.FileSystemObject")
if (fstemp.FileExists(whichFN) = True) then
Set filetemp = fstemp.OpenTextFile(whichfN, 8, false, 0)
filetemp.writeLine("<FEEDBACK>")
filetemp.WriteLine(smessage)
filetemp.writeLine("</FEEDBACK>")
filetemp.Close
set filetemp=Nothing
end if
set fstemp=Nothing
end if
end if
end if |
| |
|
The entire processing section is wrapped within the if block (Line 1
and Line 36) that checks if this form is being submitted for processing.
Lines 2 - 8 ensures that a website visitor can only enter one feedback
in one visit from the same browser. To enter another feedback, the visitor
will have to close and reopen their browser.
Lines 9 through 16 strips the entries of any html entries that could
polute the feedback file.
Line 22 ensures that this application will work irrespective of which
folder on the website it is copied into. Infact, a feedback could be
created in as many different web folders. Line 26 opens the feedback text
file in append mode (8).
Lines 27 - 29 appends the entry to the feedback text file. Each entry
in the text file is delimited within the lines
<FEEDBACK></FEEDBACK> making the file readable by an XML
procedure. This is useful for cross-website integration.
Display of Information
The contents of the storage text file is read and displayed on the
feedback page.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
sfeedback = "<font name=Verdana size=2>"
sfile = "_feedback.txt"
whichFN=server.mappath(sfile)
if (whichfN <> "") and (Not ISNull(whichFN)) then
Set fstemp1 = server.CreateObject("Scripting.FileSystemObject")
if (fstemp1.FileExists(whichFN) = True) then
Set filetemp = fstemp1.OpenTextFile(whichfN, 1, false, 0)
Do while (filetemp.AtEndofStream <> True)
strLine = filetemp.ReadLine
strLine = Replace(strLine,"<FEEDBACK>","")
strLine = Replace(strLine,"</FEEDBACK>","<hr>")
sfeedback = sfeedback & strLine
Loop
filetemp.Close
Set filetemp = Nothing
else
Set filetemp = fstemp1.CreateTextFile(whichfN, false)
filetemp.close
end if
Set fstemp1 = Nothing
end if
sfeedback = sfeedback & "</font>" |
At first access, when there is no feedback and no text file, the text
file is created (Line 17), otherwise, the file is opened in read mode
(Line 7). This means only one file (the ASP) file needs to be copied and
installed in the required directory.
The data delimited <FEEDBACK></FEEDBACK> is removed and
replaced by a line break (Line 10 and 11). Data separation can be
implemented using a different character or an image at this line instead
of the line break tag <hr>.
The string "sfeedback" contains the total feedback that have
been accumulated at the site can can be inserted anywhere on the html
section of the page within the ASP script limiter <% %>. If a
display of the feedback on the site is not desired this activity may be
ignored. In addition, Lines 1, 22, and 7-15 may be safile deleted.
To ensure that the page is always refreshed an ASP command <%
Response.Expires =0
%> is inserted at the top of the page.
A comment must be made about the appropriateness of this application on
a high volume site. It is a concern that there may be concurrency issues
in the very rare situation that users try to commit their feedback at the
same time. The chances of this situation ocurring increases with the rate
of feedback entries. However, it goes without say that this solution would
already prove undesirable given the size, volume, and time it would take
for this file to load and view in that situation.
See this application in
action.
Download the application
(zipped ASP page).
Download a NetVIOS version of the
application (See the NetVIOS version).
Copyright NetVIOS. 2001. Want to use this article? |