Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a POST submitted field exists in VBScript?

After a form is submitted, how does one check server-side if a particular field exists? For example:

If [Exists] Request("FieldName") Then
    ...
End If
like image 626
reformed Avatar asked Aug 07 '13 18:08

reformed


3 Answers

If Request("FieldName").Count > 0 Then
    ...
End If

Or, for short:

If Request("FieldName").Count Then
    ...
End If

Background:

  • The Request collection is magic, in so far as it does not throw an error when you try to access a key that was not part of the request - but the .Count will be 0 for non-existing keys.
  • In a URL-encoded query string it's legal to send keys that don't have a value, like foo&bar&baz
  • It's also legal to send the same key multiple times, i.e. multiple values per key, like foo=value1&foo=value2.

Therefore, the reliable way to determine if a key has been sent by the client is to count how many times the client has sent it.

A special case of this test is checking whether there was a non-empty value for that key (If Request("FieldName") > ""). This may or may not be what you want in the end; just be aware that the underlying behavior of query strings is broader than that.

like image 83
Tomalak Avatar answered Oct 14 '22 05:10

Tomalak


Check if it's not empty. There are a few different ways, but the one I've seen more frequently used is along the lines of:

If Request("FieldName") <> "" Then
 'etc.
End If

I usually explicitly check the Form and QueryString collections with some variation of one of the code below if I may be getting the variable from one or the other depending on context:

Select Case True
    Case Request.Form("FieldName") <> ""
        'Run if the Form isn't empty
    Case Request.QueryString("FieldName") <> ""
        'Run if the QueryString isn't empty
    Case Else
        'Set a predefined default if they're both empty
End Select

Or a nested If ... Then:

If Request.Form("FieldName") <> "" Then
    'Run if the Form isn't empty
ElseIf Request.QueryString("FieldName") <> "" Then
    'Run if the QueryString isn't empty
Else
    'Set a predefined default if they're both empty
End If

If I know exactly which collection it's coming from, I'll check that collection specifically. The reason is that I want to make sure it is pulling what I expect from where I expect it to come from. I don't want someone overriding a Form value by sending something in the QueryString when I'm not expecting it.

From MSDN:

If the specified variable is not in one of the preceding five collections, the Request object returns EMPTY.

All variables can be accessed directly by calling Request(variable) without the collection name. In this case, the Web server searches the collections in the following order:

  • QueryString
  • Form
  • Cookies
  • ClientCertificate
  • ServerVariables

If a variable with the same name exists in more than one collection, the Request object returns the first instance that the object encounters.

It is strongly recommended that when referring to members of a collection the full name be used. For example, rather than Request.("AUTH_USER") use Request.ServerVariables("AUTH_USER"). This allows the server to locate the item more quickly.

like image 13
AnonJr Avatar answered Oct 14 '22 04:10

AnonJr


To check if the parameter was present (without caring about its value) it is also possible to write:

fieldValue = Request("FieldName")
if Not IsEmpty(fieldValue) ...

One advantage over Count method above is, that you can test the variable, without referring to the field name again. Advantage over testing for "" is that if you pass &FieldName without assigning value, test for "" will yield true, but IsEmpty returns false.

Edit: Turns out this is not reliable in IIS.

  1. For the url with ?param alone, or ?param=&param2, IsEmpty(param) returns false, but
  2. For the url with ?param&param2, IsEmpty(param) weirdly returns true ...
like image 1
chukko Avatar answered Oct 14 '22 03:10

chukko