Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

asp classic - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...