Learn how to create and handle Web-based forms using ASP.
Forms collect data from the user and post it back to the server for processing. They feature in guest books, feedback pages, shopping carts, search engines, and almost all interactive websites. In this tutorial, we'll show you how you can use ASP to get at the data that's sent to the web server from a form.
All HTML forms are created using the
<form method="xxxx" action="xxxx"> (form fields in here) </form>
method attribute controls how the information that the user enters in the form is sent to the server. The two options are:
- Sends the form data as part of the URL (e.g.
"script.pl? name=Joe& email@example.com"). This is the default option. It's useful and efficient for small amounts of data (e.g. a search engine query) and it's easy for the user to refresh the results of the form by just pressing the browser's refresh button. However it cannot be used for large amounts of data (more than a few hundred bytes).
- Sends the form data encoded in the HTTP data stream. This is recommended for most types of forms (e.g. feedback forms and form mailers). The user will not see the form data in the URL. Large amounts of data can be sent this way. Unlike the
GETmethod, the user cannot easily refresh the form results page - they usually see a dialog asking if they want to resend the form data - but this is often a good thing!
action attribute specifies where the form data submitted by the user will be sent. Usually this is the URL of a script on the server - for example,
If you're thinking you recognise this part of the tutorial, that's because it's part of the ELATED HTML Forms tutorial. If you need any help on creating form fields, you might like to check out that tutorial.
We use the
Request.Querystring collection to retrieve data posted from forms that use the
GET method. The collection contains an entry for each form field posted to the server. Assume we have an HTML form as follows:
<form method="get" action="querystring.asp"> Title: <select name="title"> <option value="Mr">Mr.</option> <option value="Miss">Miss</option> <option value="Ms">Ms.</option> <option value="Mrs">Mrs.</option> </select><br> First name: <input type="text" name="firstname"><br> City: <input type="text" name="city"><br> <input type="submit" name="submit" value="Send"> </form>
We could use
Request.Querystring("title") Request.Querystring("firstname") Request.Querystring("city")
to retrieve the values entered by the user. There would be a named entry for each named form field, so the "submit" button would also result in a value being stored in
ASP provides the
Request.Form collection to retrieve data sent from forms using the
POST method. As with the
QueryString collection, the
Form collection also contains an entry for each form field posted to the server. So, taking our example form above and changing the
GET method to be a
POST, we could use:
Request.Form("title") Request.Form("firstname") Request.Form("city") Request.Form("submit")
to retrieve the values entered by the user. Sometimes you'll see ASP code where
Request.Form("field_name") has been written as
Request("field_name"). This is a valid short-hand notation, however it is usually a good idea to explicitly reference the collection you want to use - it's faster to execute and it avoids ambiguity where an item in a different collection might have the same name.
Sometimes we might have a form that contains a set of checkboxes. If we make these a group by giving them the same name, all the checked box values will be sent to the server using the same field name:
<form method="post" action="checkbox.asp"> Please check the boxes to indicate your interests:<br> <input type="checkbox" name="interests" value="film"> Film<br> <input type="checkbox" name="interests" value="music"> Music<br> <input type="checkbox" name="interests" value="theatre"> Theatre<br> <input type="checkbox" name="interests" value="sports"> Sports<br> <input type="submit" name="submit" value="Send"> </form>
Assume we checked all four boxes, we can access the values like this:
Request.Form("interests")(1) Request.Form("interests")(2) Request.Form("interests")(3) Request.Form("interests")(4)
We can also use the
Count property to find out how many values were submitted. (In the above example,
Request.Form("interests").Count equals 4.) This allows us to loop through the values using a
For ... Next loop:
For counter = 1 To Request.Form("interests").Count Response.Write "You selected " & Request.Form("interests")(counter) & "<br>" Next
(If you're not familiar with loops, you might want to read our loops tutorial.) However, usually we'd want to use code like this to retrieve the data:
For Each item In Request.Form("interests") Response.Write "You selected " & item & "<br>" Next
This loops through each of the values submitted one at a time and outputs each one. Unlike the previous
For counter = 1 To Request.Form("interests").Count ... Next loop, the example above will work even when no checkboxes were selected.
In this tutorial, we've learnt how to access form data using ASP. This is a key skill that you'll use time and again when creating websites. To make the most of forms, you'll probably want to either store the data, or email the data to someone. We'll show you how to do these in future ELATED tutorials.
Responses to this article
There are no responses yet.
Post a response
Want to add a comment, or ask a question about this article? Post a response.
To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.