Using Server Variables with Forms

One example of a useful server variable is REQUEST_METHOD. If you check it in an ASP Web page, it has a value of POST when the page is referenced by a form, and it has a value of GET when the user has referenced a page either directly or by using the Refresh feature of the browser. This is useful because if the user refreshes the ASP Web page that was used to respond to a form, the values are not set in the Request object and your page does not display correctly.

NOTE
The problem of a user refreshing an ASP Web page that was used to respond to an HTML form is not so significant if your users are using Microsoft Internet Explorer 4.0. During a refresh, this browser will display a message box ("Repost form data?") so that the end user has the option of reposting the data. Checking for GET or POST in your ASP code is still useful, however, because it covers the scenario in which the page might be accessed directly.

Figure 9-10 shows how to modify the Favorite Car form's corresponding ASP Web page, FormHandler.asp. The modified file is named FormHandler2.asp. This example tests REQUEST_METHOD and displays a message when the user clicks the Refresh button.

Figure 9-10. An example that tests the REQUEST_METHOD server variable.

<%@ Language=VBScript %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> <TITLE>Form Processor</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <% Dim RequestMethod ' reference 1 RequestMethod = Request.ServerVariables("REQUEST_METHOD") ' reference 2 If RequestMethod = "GET" Then ' This means the user refreshed FormHandler2.asp ' reference 3 %> <H1> <C>Sorry!</C> </H1> You can't use Refresh to view this screen. Use your Back button to go  to the form, change the data if need be, and press the Submit button  to get back to this point. <P> Thanks! <% Else  ' The user pressed submit on the form %> 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 %> <% End If %> </BODY> </HTML> 

The statement after the reference 1 comment gets the value of REQUEST_METHOD and stores it in a local variable. REQUEST_METHOD's value is POST when the ASP Web page is called by a <FORM> tag's ACTION attribute. Any other time the ASP Web page is called—when the user types the Web address directly into the browser, refreshes the page, or clicks on a link that is connected to the page—the value of REQUEST_METHOD is GET.

The script then checks the value of REQUEST_METHOD (the statement after the reference 2 comment). As in Visual Basic, the If construct in VBScript contains a block of code that is executed if the condition being evaluated is True; and an optional Else block that, when included, executes if the condition is False. One technique you can use in this VBScript is to place a chunk of HTML in between the VBScript If, Else, and End If statements. Even though the VBScript section is closed after the reference 3 comment, the code that follows is still part of the If block. The HTML between the If block and the VBScript section with the Else statement is executed only when the condition in the If is true, even though it is not between the <% and %> symbols. Although this technique can be slightly confusing at first, it is important and is often used in ASP Web pages.



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