Credit Guide

Search
Directory
Links


Create the future you want! Learn to make money online. Visit our website and start today!  www.exclusivebizopps.com

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 stringbut 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.

[ Comment, Edit or Article Submission ]

Share this:

Add To Windows Live Add To Slashdot Stumble This Digg This Add To Del.icio.us Add To Reddit Add To Yahoo MyWeb Add To Google Bookmarks Add To Furl Fav This With Technorati Add To Newsvine Add To Bloglines Add To Ask

More about:

Nov December 2008 Jan
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Related Blog of Credit Guide on Sphere Credit Guide Blog on Technorati

Credit Guide

Copyright © 2008 www.creditguide.org.uk. All rights reserved. Valid XHTML 1.0 Transitional

NetObjects Fusion UserFriendly Web Design Software