Hosting Guides

ASP Training - Looping and Form Handling Part 2

Looping


We have looked at form handling and now we will look at one way of looping and you will see from the example why I have bundled these two together (later you will see me bundling a different way of looping with database access). Looping is just a way of doing something repeatedly until a cetain condidtion is met, like you run out of things to do, or a counter reaches a certain value.


So. let's once again set up an HTML form and process it, there are no headings to the form elements here, just enter what data you like and select what you like.








Now that you have tried that and seen the simple output lets look at the form HTML and the ASP.


The HTML


<form action="scripts/looping.asp" method="post">
  <input type=text name="firstFormElement"><br>
  <input type=text name="secondFormElement"><br>
  <input type="Radio" name="firstRadioElement" value="one" checked>
  <input type="Radio" name="firstRadioElement" value="two">
  <input type="Radio" name="firstRadioElement" value="three"><br>
  <input type="Radio" name="secondRadioElement" value="alpha" checked>
  <input type="Radio" name="secondRadioElement" value="beta">
  <input type="Radio" name="secondRadioElement" value="gamma"><br>
  <input type="Checkbox" name="firstCheckBox" value="i">
  <input type="Checkbox" name="firstCheckBox" value="ii">
  <input type="Checkbox" name="firstCheckBox" value="iii">
  <input type="submit">
</form>


As you can see this is a fairly simple HTML form, and seeing the code you can now see where the ASP is getting its values from. Lets look at the ASP and see how the output is generated, in particular, look at the looping aspect (the for....next bits).


<html>
<head>
  <title>looping example with html form</title>
</head>
<body>
<table>
<tr>
  <th>Form Element Name</th>
  <th>Form Element Value</th>
</tr>


<%
'loop through the request.form fields and output the information
For Each field In Request.Form
%>

<tr>
  <td><%= field %></td>
  <td><%= request.form(field) %></td>
</tr>
<% next %> </table>
</body>
</html>


Note : this will be that last time that I show you the whole of the ASP output, including all the wrapping HTML; from now on I'll just include the relevant bits of the ASP, otherwise these pages will get verrrrry long. What I will do for you though is provide a link to a page that has the complete ASP incase you want to see everything in all its glory :-)


As you can see from the ASP above the active elements of the script only takes up 8 lines and only two of those are really ASP, the others are there to format the output for display.


The line for each field in request.form is the line that does all the real work in this script, and it is something that we will use again (when looking at databases and record sets) so it bears closer examination.


The request.form object is one that we have already used on this page to access individual form values (such as name = request.form("name"). We can also use the request.form object in a different way. You can think of request.form as being a collection of all the values that were submitted by the form; and you can think of lines like name = request.form("name") as a way of accessing one specific element of the collection.. However, you can also access each of the elements in the request.form collection in turn.


Knowing this makes the line for each field in request.form read like this; there are a number of elements in the request.form collect; starting with the first of these elements do the following lines of code; when finished, go through the code again, this time looking at the next element in the collection; keep doing this until there are no more elements in the collection left to look at.


Simple.


That was an overview of what the for each does; so what is happening in our piece of code (that I have just glossed over, did you notice?). Well, the first thing to make clear is what field is. All field is is a temporary place holder for each element in the request.form collection, as each element is looked at it gets stored in field so that subsequent code can use it. As a temporary place holder field can be named anything you like; I like to use meaningful variables names when I code, as such I like the word field; but it could be anything, if you feel adventurous you could replace all uses of field with jkdfioukjlsadkjlsdf and the ASP will still work quite happily.


The next thing to look at is what the code does inside the for each .... next loop.


Again this is pretty simple. Each element in the request.form collection holds two pieces of information; the name of the form element and the value of that form element (e.g. what radio button was selected or what text was typed in). So in our code, we first output field, or the name of the HTML form element that we are looking at; followed by the value (field.value) of that HTML element.


And that, as they say, is that!


This is a simple introduction, but you should be able to see that we could make a lot more serious use of the form elements than just writing them to screen, but that is for another page as I have covered enough.


Programmers Note:


If you are conversant with the POST and GET methods for form actions and understand GET and how to use it as a form action or via a query string URL you might like to know that ASP provides a way of accessing this alternative way of passing form information; there is an another request property related to form and that is querystring, so if you use a GET method to pass form data you can access it with reqeust.querysting("HTMLformelemnt").


Further programmers note :


The request object is used to access a whole host of collections of information, for example, form, querystring, cookies. If you are using any (or all!) of these methods to get information into your script then you can just use request("elementNameRequired") and ASP will search through the cookes and the form elements and the querystring (and server variables and client security certificates) until if finds the value requested. This is highly inefficient and as a programmer you should jolly well know where the informtion is coming from and code accordingly. However, it is useful to know that the search order is if a specific collection is not supplied. The search orders starts with querystring followed by form. So if you could be supplying your ASP with variables from a URL or a form posting then it might be acceptable to use the searching capabilities of the request object.


Summary


anyVariable = request.form("HTMLformelement") When an HTML form is used to submit data to an ASP page (by making the form action point to an ASP page <form action= "someASPPage.asp"method="post">), the information from this form gets stored in a collection called request.form. You request the indvidual values of each form element by giving that HTML form element name to the request.form collection
the request.form collection request.form is a collection of all the form elements names and associated values, using for each (below), you can move through each of the elements in the collection. You then have access to the name of the HTML form element (element) and the value of that HTML form element (element.value). It really is much simpler in use than in explanation :-)
for each element in anyCollection ...some code making use of element ... next If you have a collection of elements, such as an array or the request.form object a for each ... next loop can be used to move through them. Each element in the collection is temprarily stored in the whatever name you give to element and then becomes available for manipulation in the code. The next statement delimits the end of the for each code block and signifies that the next element of the collection should be looked at. When all element have been examined (or an exit for statement is executed) processing continues on the line immediately after the next.

Advertisements


Popular Countries




Choose a letter