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

前端 未结 4 2021
情书的邮戳
情书的邮戳 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 17:50

    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.

提交回复
热议问题