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:
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:
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.
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.
Share this:
More about:
- Magazine Group Magazine Subscriptions Online
- How do I iterate through a form collection?
- Need a Copy of Your Tax Return Information?
- ASP.Net Applications
- Accepting Credit Cards on your Website
- ASP TagGroup for UltraEdit32
- 12 Fundraising Sales Secrets
- How to Save on Women's Plus Size Clothing
- The Easy Way To Build A Good Credit Score




