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

前端 未结 4 2017
情书的邮戳
情书的邮戳 2020-12-11 17:33

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
<         


        
4条回答
  •  天命终不由人
    2020-12-11 18:00

    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.

提交回复
热议问题