ServersUse the Request.Form Collection

Use the Request.Form Collection

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




Sirgio Pereira

The Request.Form syntax is very simple and short:

The Request.Form syntax is very simple and short.

Request.Form(

element)[(index)|.Count]

Now let’s see how this workers for your best interest. We’ll start with a very simple page that response to user input.

— page8.asp —

Request.Form

Thanks !

Give me your name:

— End of page —

You can see a working example of the above script. When the script is first called, through a normal link, there’s no form values on it, so the call to Request.Form(“Name”) returns an empty string. The page then displays a form that contains two inputs. A text input with the name –Name– and another input of type submit with no name and no value. When the form is submited using the submit button, the same page is called with a post (note the method tag). Now the Request.Form(“Name”) has a value on it (even if it is an empty string), and (if it isn’t an empty string ) the if will trigger the thanks message. Now several questions may raise here. What happens to the submit input? And what about the case issue? Is –Name– different from –name–? Let’s rewrite the code and find some answers.

— page8a.asp —

Request.Form

Request.Form Statistics:

Number of inputs:

Request.Form(“Name”)()=

Thanks !


Form without name on submit input:

Give me your name:


Form with name on submit input:

Give me your name:


— End of page —

You can see a working example where I’ve added a Request.Form.Count to the code. This returns the number of inputs we have in the Request.Form collection. When first called, this page will indicate that there are no inputs (items on the collection Request.Form). It will also show two forms separated by hard breaks. The first one doesn’t have a name given to the submit input. If you write something on the text input and submit the form, you’ll see that there’s one input and it’s value. If you do the same with the second form, there will be two inputs, name and submit, as the two are named in the HTML. In a submit type input, if you give the name, the pair name/value will be given by the browser to the web server. The question is –name different from –Name– when using Request.Form is answer looking at how I wrote it when saying Thanks.

Let’s move on to another topic. How do we handle a select box with multiple choices enabled? One more time, let’s rewrite our code and see what happens.

— page8b.asp —

Request.Form

Thanks !

Request.Form(“Name”)()=

Give me your name:
Sergio
Pereira
Stephen
Wynkoop
Oberonsis

— End of page —

You can see this script working. With the multiple attribute, the select can return several or none of the options. Try to select various ones and see the result. Request.Form(“Name”).Count will return the number of options selected and the for loop will show you each one in turn calling Request.Form(“Name”)(k), where k is the index of the option selected. Note that if no option is selected Request.Form(“Name”) will be an empty string and Request.Form(“Name”).Count will return 0. If there are several of options selected, then Request.Form(“Name”) will return all of them concatenated and separated by virgules, Request.Form(“Name”)(k) will return the k option selected. Note that the index k should be between 1 and Request.Form(“Name”).Count.

Let’s move on now on how can you use that Request.Form to judge future actions of your script. A simple one that will say –Welcome — if the you click –Enter– and –Goodbye– if you click –Leave–.

— page8c.asp —

Request.Form

If Not Request.Form(“Name”)=”” Then
Select Case Request.Form(“action”)
Case “Enter”
%> Welcome !

Case “Leave”

%> Goodbye !

End If %>

Give me your name:<input type="text" name="Name" value="”>

— End of page —

See it. We have two submits with the same name but with two different values. Only the submit value that you click will be given to the server, so we can make a test on the value on it and act accordingly.

As you can see, working with th Request.Form collection is very simple. There’s still something that should be said about it. When using check boxes on a HTML form, you should test it with

If Request.Form(“checkbox”)=”” Then
–Code to do when checkbox selected
Else
–Code to do when checkbox is not selected
End If

as it returns –on– when selected and nothing when not.

I want your feedback! Mail me!

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories