Interacting with HTML Forms and Pages

After looking at the new method for working with forms to collect and edit data via the FormManager DTC, we'll now cover the original Visual InterDev 1 method which is still supported in Visual InterDev 6. This older technique is still important because it can often be simpler to program and gives you more manual control over the code that you write. The FormManager DTC is typically used for more complex data entry and editing routines that involve multiple modes. The HTML form technique is used for simpler data capture requirements. An example might be where there is no need for multiple modes and where there is no need for navigation through a Recordset DTC.

This section describes the primary way to get user information with the Form collection. We'll also briefly touch on the QueryString and ServerVariables collections. These are just three of the collections available in the Request object in the Active Server Framework. For more information on the other collections, namely the ClientCertificate and Cookies collections, see Chapter 10 or the online documentation that comes with Visual InterDev.

The Form Collection

One of the most common ways to obtain input for a server-side application is from an HTML form. Forms are part of standard HTML, and virtually all modern browsers support them. Forms let users of your HTML page enter information using text boxes, check boxes, radio buttons, and list boxes. Users can then submit this data by clicking a command button on the form. The form then embeds the information within an HTTP request string and sends the string back to the server.

Traditionally, the return string has been passed to a CGI application (an .exe file running on the server). The CGI application then parses the string and interprets the results. Many aspects of this technique make it cumbersome. The CGI application is often written in C. This means you must be familiar with an additional language, use a separate set of tools, use a complex debugging process involving unintegrated components, and then interpret the HTTP request. You may have seen the strange text that your browser adds to the URL when sending a request to the server after you click the Submit button on a form. For instance, the following text is sent to the Microsoft search site when you try to search for "Space Exploration near mars" and select Excite as the search engine:

http://www.excite.com/search.gw?searchType=Concept&category=     default&mode=relevance&showqbe=1&display=msn,hb&search=     %22Space+Exploration%22+near+mars 

The CGI programmer must know the rules of HTTP requests and be able to parse the data out of a string such as that shown above. This can be a tedious process.

The Request object in Active Server Script lets you circumvent all these complications. First, it lets you access the data from within the ASP Web page and keep the entire process inside Visual InterDev. Second, it parses the HTTP request for you, making the data readily available and easy to use.

Processing Forms with ASP Files

An ASP file lets you collect or process HTML form values in three ways:

  • A static .htm file can contain a formCX that posts its values to an ASP file.
  • An ASP file can create a form that posts information to another ASP file.
  • An ASP file can create a form that posts information to itself—that is, to the ASP file that contains the form.

The first two methods operate the same way as forms that interact with other gateway programs, but with ASP you can include commands that read and respond to user choices. The third method is more complicated, requiring logic to handle different states within the file. Creating an ASP file that contains a form definition that posts information to itself is a slightly more complicated but powerful means of working with forms.

NOTE
If you are considering writing a self-posting ASP Web page, you might want to consider using the FormManager DTC instead since your page is most likely multimodal (that is, having multiple entry paths).

The first step in processing forms with ASP is to create the appropriate HTML form (which can itself be an HTML or ASP file) and tell the form to send its data to the form handler ASP file. You do this in the HTML <FORM> tag by specifying the path of the ASP file that handles the processing, as shown here:

<HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Favorite Car</TITLE> </HEAD> <BODY BGCOLOR="#fffff"> <FORM METHOD=POST ACTION="FormHandler.asp"> <P> First Name: <INPUT NAME="FName" SIZE=30> <P> Last Name: <INPUT NAME="LName" SIZE=30> <P> Which car do you like best? <SELECT NAME="Car"> <OPTION>Porsche <OPTION>BMW <OPTION>Ferrari <OPTION>Chrysler <OPTION>Lamborghini </SELECT> <P> <INPUT TYPE=Submit> </FORM> </BODY> </HTML> 

This form produces the page shown in Figure 9-7 (which is in Quick view mode in Visual InterDev).

click to view at full size.

Figure 9-7. A simple HTML form.

The <FORM> tag tells the browser to collect the data the user enters on the form and package it in an HTTP request. If you weren't using ASP, a CGI program would be specified in the ACTION attribute. The following syntax specifies the ASP file named FormHandler.asp:

<FORM METHOD=POST ACTION="FormHandler.asp"> 

When the user clicks the Submit button on the form, control is transferred to FormHandler.asp. (See Figures 9-8 and 9-9.)

The HTML form has three inputs: the text box named FName, the text box named LName, and a list box named Car. When the user clicks the Submit button, the names of all the form's inputs are passed to the target specified in the form tag's Action qualifier, as are the values in the form's controls. Figure 9-8 shows how to retrieve the first and last name from the form, as well as the name of the car that the user selects as his or her favorite. The code shows how you can use this information to make a decision about what output to send to the user.

Figure 9-8. Retrieving information from a form by using the Request object is a straightforward process.

<%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Form Handler</TITLE> </HEAD> <BODY BGCOLOR="#FFFFF"> Welcome, <% = Request.Form("FName") & " " & Request.Form("LName") %>. <P> Your favorite car is a  <% DIM favcar favcar = Request.Form("Car") If favcar = "Chrysler" Then     Response.Write("... wait a minute... there must be an error.")     Response.Write("<P>A <B>Chrysler</B>?") Else     Response.Write(favcar & ".<P>That's cool.") End If %> </BODY> </HTML> 

In the ASP Web page, you can reference the values the user enters by using the Form collection of the Request object.

The syntax for retrieving the variables from the Form collection is

Request.Form(parameter)[(index)|.Count] 

You retrieve the first name and last name values using the following syntax, which outputs the form's variables to the HTML stream with a space between them:

<% = Request.Form("FName") & " " & Request.Form("LName") %> 

The Car form variable is retrieved in the same fashion:

favcar = Request.Form("Car") 

If the HTTP request contains more than one parameter with the same name, it forms a collection that you can index by a number. For instance, if the HTML form has two first name text boxes named FName, you can access them in your script like this:

The first FName is <% = Request.Form("FName")(1) %> And the second FName is <% = Request.Form("FName")(2) %> 

If you do not use the index on such a parameter, you get a string with both values separated by commas. You can use the Count property to determine how many form variables with a particular name are in the Request's Form collection. For instance, if the HTML form has two FName variables, the value of the following is 2.

Request.Form("FName").Count 

Self-posting pages

You can easily create an ASP Web page with a form that posts the input values back to itself. The user can then fill in the form variables and submit the form. The values are sent along with the request to reload the ASP file. The ASP file simply uses the Request object to access the form variables. The ASP file can check the results and update the form with a message if necessary. Also, self-posting forms are great for pages that progressively build as the user completes the form. For instance, when a user selects a type of car from the list, the page redisplays with a list of submodels. If the user then selects one of the submodels, the page redisplays with another list of features for the submodel. You can handle each step of the page in the same ASP file to give the user a seamless view of the application.

Self-posting pages work much more seamlessly than separate pages that are sent back to users with some type of message—separate pages require users to move back to the previous page. Self-posting forms can display the message and let users continue.

Self-posting pages require only one type of special logic—the logic to handle the different states the page might take. The page must be able to distinguish between each mode it might take. For instance, a page typically has one mode to display one section of the page and another mode to process the section.

The GetEmail.asp page from the online Visual InterDev documentation is a simple implementation of a self-posting page. This page checks the Email variable for several things. If Email is blank, the resulting page asks the user for his or her e-mail address. If Email does not contain the @ character, the user sees the same message with a note on the proper syntax. If Email does not meet one of the last two criteria, it's probably OK and the final message appears. Figure 9-9 shows the code for GetEmail.asp.

Figure 9-9. An example of a self-posting page.

<HTML> <BODY> <!-- GetEmail.asp --> <%      If IsEmpty(Request("Email")) Then          Msg = "Please enter your email address."      ElseIf InStr(Request("Email"), "@") = 0 Then          Msg = "Please enter an email address" & _             " in the form username@location."     Else         ' In a real application, the following message         ' would be replaced by actual processing.         Msg = "This script could process the " & _             "valid Email address now."     End If %> <FORM METHOD="POST" ACTION="GetEmail.asp"> <PRE> Email: <INPUT TYPE="TEXT" NAME="Email" SIZE=30      VALUE="<%= Request("Email")%>"> <%= Msg %><P> <INPUT TYPE="Submit" VALUE="Submit"> </PRE> </FORM> </BODY> </HTML> 



Programming Microsoft Visual InterDev 6. 0
Programming Microsoft Visual InterDev 6.0
ISBN: 1572318147
EAN: 2147483647
Year: 2005
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net