//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: Forms :: Which should I use: Request("item") or Request.Form("item")?


Which should I use: Request("item") or Request.Form("item")?

According to Microsoft, you should ALWAYS use the complete name of the collection you are retrieving items from. For more information, read the following resource in its entirety: 
 
    Request Object 
 
A lot of people leave out the actual collection name, merely putting request("item") either out of laziness or because they're not sure which collection the value will come from. In my opinion, it is better programming practice to either (a) use one method exclusively, or (b) do the following in such cases where you can't avoid using multiple submission methods: 
 
<% 
    Function Req(item) 
        Req = "" 
        If Request.QueryString(item).Count > 0 Then 
            Req = Request.QueryString(item) 
        ElseIf Request.Form(item).Count > 0 Then 
            Req = Request.Form(item) 
        End If 
    End Function 
%>
 
A slightly less efficient method would not use the collection's count property, but checking Request.ServerVariables("REQUEST_METHOD"). Still another way would be to check if the specific item is an empty string—but this can be misleading, because the result will be the same if an empty string is passed, or if that variable name doesn't exist in the collection at all. 
 
The reason you should always use an explicit name is that, if you don't specify, you can't be certain which collection the variable is coming from (it also can be a much more significant performance hit to poll all of the collections looking for your variable). Take the following example - play with each form, and see which collection is used as the 'default' in each case: 
 
order.asp 
-------- 
 
<% 
    response.cookies("HTTP_USER_AGENT") = "cookie" 
%> 
<form method=post action=order2.asp?HTTP_USER_AGENT=get> 
    <input type=hidden name=HTTP_USER_AGENT value=post> 
    <input type=submit value='form with both'> 
</form> 
 
<form method=post action=order2.asp?HTTP_USER_AGENT=get> 
    <input type=submit value='form with get'> 
</form> 
 
<form method=post action=order2.asp> 
    <input type=hidden name=HTTP_USER_AGENT value=post> 
    <input type=submit value='form with post'> 
</form> 
 
<form method=post action=order2.asp> 
    <input type=submit value='form with neither'> 
</form> 
 

 
order2.asp 
---------- 
 
<% 
    Response.Write "The first value is from: " & _ 
        Request("HTTP_USER_AGENT") 
%>
 
Note that in each case, the collection changes. If you had avoided using the cookie in the first line of the page, you would have received the value in Request.ServerVariables("HTTP_USER_AGENT") only in the last case (where that variable name was not used in the Form or QueryString collections). 
 
So, always name the collection you are referencing, instead of letting the engine figure it out for you. At most, it's 15 more characters... and if you're doing it often enough, you could create a function for each collection to ease your poor fingers.

Related Articles

How can I mimic a client-side POST from ASP?
How do I cause/prevent ENTER being used to submit a form?
How do I change the target frame or window of a response.redirect?
How do I disable certain FORM elements?
How do I disable IE's Autocomplete feature?
How do I iterate through a form collection?
How do I make form fields read-only?
How do I make one dropdown depend on another?
How do I pass x-y coordinates to ASP, after the user clicks an image?
How do I perform spell checking from a web page?
How do I retrieve the name of the form that was submitted?
How do I retrieve the text and the value from a <SELECT> element?
How do I submit forms to a new window, with window.open() features?
How do I upload files from the client to the server?
How do I validate a credit card number in ASP?
How do I validate forms using server side script?
What is the limit on Form / POST parameters?
What is the limit on QueryString / GET / URL parameters?
What is the size limit of a posted FORM field?
When I have multiple submit buttons, how do I tell which was clicked?
Why can't I access Request.Form() when uploading?
Why can't I pre-populate INPUT TYPE=FILE element?
Why do I get 'HTTP 405 - Resource Not Allowed' errors?
Why does my form variable become 'value, value' instead of 'value'?
Why does my input type=text value get truncated?
Why won't my <TEXTAREA> display the data I passed to it from ASP?

 

 


Created: 3/16/2001 | Last Updated: 1/22/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (282)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...