Subscribe to Windows IT Pro
January 06, 2000 10:14 AM

Active Server Pages Takes the Bite out of Forms

Windows IT Pro
InstantDoc ID #7951
Rating: (0)
Downloads
7951.zip

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:

<p>The time is <%=time()%> </p>

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

<p>The time is 03:16:32</p>

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.

Related Content:

ARTICLE TOOLS

Comments
  • Bradley Young
    11 years ago
    May 29, 2001

    I, too, am very interested in this article as it describes exaclty what I require, but do not have the time (at the moment) to learn how to do it fully. However, upon running the page, despite copying exactly (at least I'm fairly sure), I get the following error:

    Microsoft VBScript compilation error '800a0400'

    Expected statement

    /bpyoung/addtext.asp, line 14

    option explicit

  • James Taylor
    12 years ago
    Jun 22, 2000

    This message is more for James R. Gregg. You might try the following to "clean up" your issue.
    1. Make sure you end up with three files (form.html, addtext.asp and feedback.txt)
    2. Place "Feedback.txt" into your C: directory, the other two into a directory in your wwwroot/inetpub folder. I placed mine in a folder named "CODE"
    3. Make sure you have properly "shared" that folder for ASP purposes.
    4. Access the files by typing http://localhost/CODE/form.html
    5. Enjoy playing with the app.
    I think your issue may have arisen out of not having your "ASP" file properly named. If you do it in notepad, you must make sure when you save your file that you select "all files" and not "text files" from the drop down box at th ebottom, otherwise your file could get saved like this 'addtext.asp.txt" which your computer will not process.

    Good Luck

  • Dan M
    12 years ago
    Jun 15, 2000

    The article incorrectly states: "ASP scripts work only on Web servers running IIS or machines running Peer Web Services (PWS) 3.0." This is not accurate. Chili!soft has a product called Chili!ASP that allows ASP scripts to run on several flavors of windows. htttp://www.chilisoft.com

  • Cliff Airhart
    12 years ago
    Apr 27, 2000

    Mark Minasi's Inside Out: "Active Server Pages Takes the Bite out of Forms" (February 2000) is a great article. What the article describes is exactly what I want to do. Will you send me the three files you reference in the article (form.html, addtext.asp, and feedback.txt)?
    --­Cliff Airhart

  • Mark Minasi
    12 years ago
    Apr 27, 2000

    Here's a quick way to get form.html and addtext.asp: Go to Windows 2000 Magazine's online archive (http://www
    .win2000mag.com/articles), scroll down and enter 7951 in the InstantDoc ID text box, and click Download the Code/Files in the Article Info box. Feedback.txt is just a blank file to start with, so you don't need to download it.
    --­Mark Minasi

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.