10 year anniversary

Home


Hosting Directory


Host Search


Request a Quote


Web Hosting News


Hosting Forums


Web Host Awards


Guides
Web Hosting
Choose a Host
Dedicated Servers
Reselling
Free Hosting
Articles
Industry News
HTML Explained
Advanced HTML
ASP Explained


Free Listings on Web Host Directory and drive buyers to your Web Hosting Business





ASP Explorer Back to Articles & Guides 
ASP Training - Looping and Form Handling

By this point you are should have a fair grasp of what active server pages involve, be able to recognise an asp page when you see one (by the .asp extension on the URL or by the <% if access to the source code on the web server).

This page will be concerned with processing HTML forms using ASP. I will also start to introduce you to some of the "looping" mechanisms that come with ASP to make your life easier.
(We will use loops quite a lot in the later pages when dealing with updating and displaying information from a database.)

Warning

This is an ASP guide. Whilst some elements of HTML form use will be touched upon and we will use HTML form elements this is not going to be an HTML form trainer; I am going to assume you have sufficient HTML form knowledge to cope; or that you are clever enough to work out what is going on by viewing the HTML source of this page. If neither of these statements fit you then now would be a good time to surf the web for some more information; there are loads of web sites that are available to help you learn HTML.

Example Form

Fill in your name, pick an option and click a button to see ASP form handling in action.
Your Name Is? :

Pick an option :

Submit the form using one of the buttons below :




Not very exciting is it? But this is a training guide so what did you expect?  Anyhow,  let's have a look at how this ASP is working. First take a look at the HTML source, then look at the ASP to see how it is processed. The HTML The following is an excerpt from the HTML to display the form and not the whole HTML page itself.
<form action="scripts/processExampleForm.asp" method="post">
Your Name Is? : <input type="text" name="name" size="20">
<br>
Pick an option : 
<select name="exampleSelection">
	<option selected>One
	<option>Two
	<option>Three
	<option>Four
</select>
<br>
Submit the form using one of the buttons below :
<br>
<input type="submit" name="submitButton" value="Btn One">
<input type="submit" name="submitButton" value="Btn Two">
<input type="submit" name="submitButton" value="Btn Three">
</form>


This is a fairly simple form. There is a free text box to enter your name; a pick list to select a number and three submit buttons, with different values for each button. This information is passed to the ASP script identified in the <form...> section, specifically the "action" attribute of the form tag.

The ASP

<% 
 name = request.form("name")
 optionSelected = request.form("exampleSelection")
 whichButton = request.form("submitButton")
%>
<html>
<body>
Hello <%= name %>,
you selected <%= optionSelected %>
and submitted the form using <%= whichButton %>

</body>
</html>
Looking through this script you ought to be able to pick out what is happening fairly easily. The first ASP section gets the information from the form and stores this in three variables name, optionSelected, whichButton ; this information is then output to an html page, exactly as was explained in ASP Training : The Basics. So, lets take a closer look at the first three lines then.

As well as being a scripting (programming) language, ASP/VBScript comes with a number of objects to allow you to access information easily and in a consistent manner (we will meet more of these later when we look at the database component).

One of the standard objects provided is the request object; the request object allows you to request information from the browser and the web server. In the case of request.form you are requesting information about the data being passed from the form; you can also use request to access information stored in cookies, the query string and server variables (if all this means nothing to you, don't worry, we will only be thinking about the .form parts of the request object).

To get access to the values being passed by the HTML form, you plug in the name of the HTML form element that you want to get hold of. So in our HTML form, we created a name form element, an exampleSelection form element and a submitButton form element.

It is as simple as that, to get the value of an HTML form element into your ASP script, you just use the request.form object and put the name of the form element into the brackets, so that you end up with
<% 
 name = request.form("name")
 optionSelected = request.form("exampleSelection")
 whichButton = request.form("submitButton")
%>
And now we have the variables name, optionSelected and whichButton in our ASP script ready to be used in any way we see fit.

Simple.

As this guide continues you will see more complex manipulation of form elements that just echo'ing them back to the page; but suffice for now that you know how to get hold of the form information and store it in variables for you to use at a later date.

Warning

A very common mistake when working with ASP and HTML forms is to forget the METHOD attribute. If you forget to put method="post" in your HTML form then the request.form will not work (there is a related programmers note before the summary on the next page).


ASP Training - Looping and Form Handling Part 2 >>

ASP Training - The Basics Table of Contents

Featured Hosts
Bytehouse.co.uk: No Set Up Fees, no minimal contract, 2 Months Free on all Shared, VPS, & online backup Annual Plans, Full range of web hosting solutions, 30-day money back guarantee
Fast Hosting Direct: 2 Year No Limits UK Hosting with Free Domain & Website Builder for only £19.99 - #1 UK Budget Hosting Award Winner - No Limits on Webspace & Data Transfer - 60 Day Money Back & 99.99% Uptime Guarantee
UK2 Web Hosting from £49.99 / year: With 1 MONTH FREE! With free setup, free domains, massive space and bandwidth.
Fasthosts: Web hosting, personalised email and great value domain names all from Fasthosts - the UK's number 1 for web hosting.
Prime Hosting Ltd: UK (London) based Linux hosting with PHP, MySQL etc. Email with Spam Protection. 24/7 Support Helpdesk
£1 Web Hosting from Namesco: Great Value, Fully Featured Hosting Packages. FREE Domain Name & Google AdWords Voucher with every Hosting Plan. Fast UK Based Support.
DediPower: Dedicated to managed hosting and co-location solutions. Delivering service excellence with 27/7 support and industry leading SLA. Support with passion.
Heart Internet: Reseller Web Hosting Specialist. Host Unlimited Web Sites With Unlimited Data Transfer, Webspace and Mailboxes for £29.99 per month
NaviSite: Leverage our London data centre for Application Services and Full-Stack Hosting Solutions with 24/7 Support. NaviSite: Run with Us.
Virtual Server Warehouse: No Set Up Fees, no minimum contract, VPS from £14.99, 30-day money back guarantee and fantastic support!

Most Popular Web Hosting Searches Advertisers, Get Listed Here. 
123-reg.co.uk
1500MB data transfer, 15 POP3s, Webmail, Web stats, full FTP access... more
Bargain Host
UK Web Hosting from £0.99 pence a month, all hosting plans come with a FREE Domain Name Registration or Domain Transfer. All ... more
Lewcy UK Hosting
UK only website hosting from Lewcy, Simple, Affordable, Reliable. UK based? UK audience? Then why choose a host on the other ... more



Windows Dedicated Hosts
Rackspace
Award Winning Managed Hosting. Live Support 24-7 and a ... more
Memset Dedicated Hosting
Want the power and security of a Dedicated Server but ... more
UKHost4u.com
30 Day Free Trial on all VPS Servers. Try us today !... more
Unix Less than 500MB
123-reg.co.uk
Free domain name with web hosting only £2.99/m. Limited ... more
Bargain Host
UK Web Hosting from £0.99 pence a month, all hosting plans ... more
Lewcy UK Hosting
UK only website hosting from Lewcy, Simple, Affordable, ... more
Unix/Linux Shared Hosts
123-reg.co.uk
Free domain name with web hosting only £2.99/m. Limited ... more
Bargain Host
UK Web Hosting from £0.99 pence a month, all hosting plans ... more
Ultimate Web Hosting
Extremely cheap for Unlimited Bandwidth Web Hosting. We ... more
advertisers: Get listed here
> About us
> Terms & Conditions
> Privacy Policy
> Advertising
> Contact us
> Forum Archive
> Sitemap
> RSS  RSS
RECOMMENDED BY
  Copyright © 1997-2008, Web Host Directory Ltd.  Web Hosting Directory