Downloads
7951.zip

I occasionally have to build a Web site, an experience that is always humbling. My problem is that I don't do Web work often enough to build any real expertise, and the time between my Web adventures is usually long enough that I forget everything I learned on the previous project. Fortunately, HTML tags simplify Web work. And even if you forget the tags, you can use Microsoft Word or Microsoft FrontPage to handle them. But gathering information from forms for use on a Web page is a hassle.

I don't want to learn Perl or any other Common Gateway Interface (CGI) programming tool to handle forms. I maxed out at 20 programming and macro languages, and I refuse to learn any more. I usually let FrontPage's built-in form bot handle the forms. Although FrontPage is a good program, getting FrontPage's Internet Information Server (IIS) extensions to work is difficult. Even FrontPage 2000's form bot is rather fragile. Downloading and installing the FrontPage 2000 IIS extensions is a challenge. As I performed this task, I worried about disaster recovery—did I want to add a step to rebuilding my Web server? I wanted to avoid using the extensions, so I searched for a way to easily collect and store form information without using heavy-duty programming. Then I remembered Active Server Pages. ASP lets you use VBScript to build server-side Web-based applications. VBScript, a variant of Visual Basic (VB), is ideal for occasional programmers.

The Task
I needed to collect comments and stories about software bugs that readers had submitted to my Software Conspiracy Web site (http://www.softwareconspiracy.com). I wanted the ASP script to collect three identification fields (name, telephone number, and email address) and a comment field, surround each field with double quotes, put commas between the fields for ease of importing to programs such as Microsoft Excel or Microsoft Access, and append the new information to an ASCII file called feedback.txt.

I feared that the project might be time-consuming, but it took only about an hour and a half. For illustrative purposes, I've summarized only the program's essentials. To save space, my example doesn't include error-handling code. You might find this example useful on your next Web project that involves forms.

In the example, I display a simple form that asks for a user's name and email address. When the user submits this information, ASP shows the information to the user and stores it in feedback.txt.

In my project, I used three files: the HTML file with the form (form.html), the ASP file to collect the data and write it out (addtext.asp), and the file to write the data to (feedback.txt). The HTML file looked like the following file:



Name



Email





The HTML

tag takes two parameters: method="POST" and action="addtext.asp". The first parameter is standard, but the second parameter is unique. The second parameter is the part of the
statement that tells the user's Web browser to ask the Web server to start the addtext.asp program. If this program isn't in the same directory as the form, you need to provide the full pathname. The

(i.e., paragraph) and tags simply display descriptive text (e.g., name) on the screen and put fields on the browser so that users can enter their responses. The last two tags tell the browser to put a Submit Info button and a Clear button on the screen. You don't need to do any programming to make these functions work; they're built into the HTML

commands.

In my example, the ASP file looked like the following file:

<%@ Language=VBScript %>

<%

option explicit

dim fso, myfile, uname, email

set fso = CreateObject _

("Scripting.FileSystemObject")

set myfile = fso.OpenTextFile _

("c:\feedback.txt",8,0)

uname = request.form("Username")

email = request.form("UserEmail")

myfile.writeline(uname & " " & email)

myfile.close

set myfile = nothing

%>

Name=<%=request.form("username")%>

Email address=<%=request.form("useremail")%>

ASP files usually contain regular HTML tags and VBScript programming commands. You place the programming commands within <% and %> brackets. ASP scripts work only on Web servers running IIS or machines running Peer Web Services (PWS) 3.0.

When a browser asks an IIS system to deliver a Web page with an HTML or HTM extension, the IIS system simply retrieves the file and transfers the file to the browser. In this case, the IIS system is nothing more than a file server.

In contrast, when a browser requests a file with an ASP extension, the IIS system checks the ASP file closely, finds the VBScript commands in the HTML code, and executes the VBScript commands. Running those commands usually modifies the HTML on the Web page; the resulting HTML is what the browser receives. IIS doesn't send the VBScript code (thus preventing people from easily figuring out how you set up your site), just the HTML that results from a combination of the original HTML and the text that added when the VBScript commands ran.

For example, consider the following one-line ASP file:

The time is <%=time()%>

Before transmitting this file, IIS sees the command =time(), which tells IIS to run the built-in VBScript time function (i.e., the function that returns the current time) and insert the result into the HTML. The resulting HTML that the user sees is

The time is 03:16:32

The ASP file for my solution has a large portion of VBScript at the beginning and a small amount of HTML at the end. The main job of the resulting Web page is to add the collected form text to feedback.txt, but the page also displays the name and email address that the user entered.

The first two statements in the ASP file tell IIS that the script is VBScript (other languages also work in ASP scripts) and that variables must be declared before they are used in the script. In general, you don't need to declare variables in VBScript. However, many people like to declare variables for organizational purposes and to decrease the probability of a mistyped variable name causing a bug. The Option Explicit statement tells IIS to enforce variable declaration.

The two Set statements let you manipulate files on the server. The first Set statement creates a file-system object, which activates the programming language's tools that allow file reads and writes. The second Set statement's parameters provide the name of the file you're working with (the default path is the \winnt\system32 directory), signal that you want to append data to the file (1 signifies read; 8 signifies append), and specify that the data must be in ASCII rather than Unicode. (For the append process to work, the file that you're appending must exist. Create an empty feedback.txt file before you try to execute the OpenTextFile method.)

The uname and email variables receive the user's name and email address. Request.form is a built-in VBScript method. To retrieve the user's information, you must pass the form's field names, "Username" and "UserEmail". You need to surround the form's field names (which must exactly match the names in the input type= statements in the HTML file) with double quotes.

The myfile.writeline method writes a string of characters to a file. In this case, the method writes the user's name and email address (separated by a space) to feedback.txt and starts a new line.

The myfile.close method closes the file. Finally, the last Set statement supports cleanup on the ASP server.

Use As You See Fit
I used this method because I've had trouble with FrontPage's server extensions and because I'm familiar with VB. However, my method might not work for you. VBScript isn't particularly fast, so you might not want to use it for a busy Web site. (My Software Conspiracy Web site has only about 10 visitors a day, so server performance wasn't a consideration.)

Although my example saves the data in a simple form, you might prefer to collect and save data in a Microsoft SQL Server database. If you're thinking about doing some ASP scripting, see Microsoft's Developer Network Web Workshop Web site (http://www.msdn.microsoft.com/ workshop/c-frame.htm#/workshop/server/default.asp) for links to ASP information. For additional ASP information, see SQL Server Magazine.

Corrections to this Article:
  • Inside Out: “Active Server Pages Takes the Bite out of Forms” (February 2000) contains an Active Server Pages (ASP) file in which some lines of code wrap across two lines of text but don’t contain a continuation character sequence ( _). A correct version of the file is available for download at http://www.win2000mag.com/articles. Enter 7951 in the InstantDoc ID text box and click on the Zip file in the Article Info box.