Web Host Directory Web Host Directory

AU

Service ChannelsWeb Host Directory

Web Host Directory

Hosting 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="true">One</option>
<option>Two</option>
<option>Three</option>
<option>Four</option>
</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).


Advertisements


Popular Countries




Choose a letter