Use the Request.Form Collection

By ServerWatch Staff (Send Email)
Posted Nov 1, 1998


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 --

<html>
<head>
<title>Request.Form</title>
</head>
<body>
<% If Not Request.Form("Name")="" Then %>
Thanks <%=Request.Form("Name")%> !<br>
<% End If %>
<form method="post" action="page8.asp">
Give me your name:<input type="text" value="" name="Name">
<br>
<input type="submit">
</form>
</body>
</html>

-- 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 --

<html>
<head>
<title>Request.Form</title>
</head>
<body>
Request.Form Statistics:<br>
Number of inputs:<%=Request.Form.Count%><br>
<%For k=1 To Request.Form("Name").Count%>
Request.Form("Name")(<%=k%>)=<%=Request.Form("name")(k)%><br>
<%Next%>
<% If Not Request.Form("Name")="" Then %>
<br>
Thanks <%=Request.Form("name")%> !
<% End If %>
<hr>
Form without name on submit input:<br>
<form method="post" action="page8a.asp">
Give me your name:<input type="text" value="" name="Name">
<br>
<input type="submit">
</form>
<hr>
Form with name on submit input:<br>
<form method="post" action="page8a.asp">
Give me your name:<input type="text" value="" name="Name">
<br>
<input type="submit" name="submit">
</form>
<hr>
</body>
</html>

-- 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 --

<html>
<head>
<title>Request.Form</title>
</head>
<body>
<% If Not Request.Form("Name")="" Then %>
Thanks <%=Request.Form("Name")%> !<br>
<%For k=1 To Request.Form("Name").Count%>
Request.Form("Name")(<%=k%>)=<%=Request.Form("name")(k)%><br>
<%Next%>
<% End If %>
<form method="post" action="page8b.asp">
Give me your name:<select name="name" size="5" multiple>
<option>Sergio</option>
<option>Pereira</option>
<option>Stephen</option>
<option>Wynkoop</option>
<option>Oberonsis</option>
</select>
<br>
<input type="submit">
</form>
</body>
</html>

-- 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 --

<html>
<head>
<title>Request.Form</title>
</head>
<body>
<%
If Not Request.Form("Name")="" Then
Select Case Request.Form("action")
Case "Enter"
%> Welcome <%=Request.Form("Name")%> !<br>
<%
Case "Leave"
%> Goodbye <%=Request.Form("Name")%> !<br>
<% End Select
End If %>
<form method="post" action="page8c.asp">
Give me your name:<input type="text" name="Name" value="<%=Request.Form("name")%>">
<br>
<input type="submit" name="action" value="Enter">
<input type="submit" name="action" value="Leave">
</form>
</body>
</html>

-- 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!

Page 1 of 1


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.