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

After submitting the form, how to check the server side if a specific field exists? For instance:

If [Exists] Request("FieldName") Then ... End If 
+6
source share
4 answers

Check if it is empty. There are several different ways, but the one I saw most often looks like this:

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

I usually explicitly check the Form and QueryString collections with some change to one of the codes below, if I can get a variable from one or the other depending on the 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 what collection he is going to, I will check this collection on purpose. The reason is because I want to make sure that it pulls what I expect from where I expect it to happen. I don't want anyone overriding the Form value by sending something to a QueryString when I did not expect this.

From MSDN :

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

Access to all variables is possible directly by calling Request (variable) without a collection name. In this case, the web server searches for the collection in the following order:

  • Querystring
  • The form
  • Cookies
  • ClientCertificate
  • ServerVariables

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

It is strongly recommended that the full name be used when referring to members in the collection. For example, instead of Request. ("AUTH_USER") uses Request.ServerVariables ("AUTH_USER"). This allows the server to find the item faster.

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

Or, in short:

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

Background:

  • The Request collection is magic, because it does not cause an error when trying to access a key that was not part of the request, but .Count will be 0 for nonexistent keys.
  • In the query string encoded in the URL, it is legal to send keys that do not matter, for example foo&bar&baz
  • You can also send the same key several times, i.e. several values ​​for each key, for example foo=value1&foo=value2 .

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

A special case of this test is to check for a non-empty value for this key ( If Request("FieldName") > "" ). This may or may not be what you want in the end; just keep in mind that the basic behavior of query strings is wider than that.

+18
source

I usually check the value of the SUBMIT button. If it was clicked, this value is published along with the form data. Thus, even if all the form data is empty, the value of the submit button will not be. And if the value of the submit button is empty, then it was not pressed.

 if request("btn_Submit") <> "" Then response.write "form was submitted" end if 

This is harder if you use the javascript form.submit () call, in which case I usually select a hidden field.

0
source

To check if a parameter is present (without worrying about its value), you can also write:

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

One of the advantages of the Count method described above is that you can test a variable without having to access the field name again. The advantage over testing for "is that if you pass & FieldName without assigning a value, the test for" "will give true, but IsEmpty returns false.

Edit: Turns out this is unreliable in IIS.

  • For url with? param alone or? param = & param2, IsEmpty (param) returns false, but
  • For a URL with param and IsEmpty (param), weirdly returns true ...
0
source

Source: https://habr.com/ru/post/951246/


All Articles