CoverYourASP --> Implementing forms with ASP --> Part 2

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
99 active users
2253 visitors today
1964 pages today
(only part of today)
Tools I use

Copyright © 1999-2016 James Shaw.
All rights reserved.

RSS submissions

Now open source with SourceForge!

The <form> framework

var bSubmitted = (Request.Form.Count > 0);
var sName = 'egghead';

// has the form been submitted?
if ( bSubmitted )
   // get the data from the form...
   sName = ''  + Request.Form ( "name" );

I start by testing the amount of data in the Form collection of the Request object. If there is some (if Count is greater than zero) then the form has been "submitted".

This means that the form has already been displayed, filled in by the user, the submit button pressed and the data sent to the page that was referenced by the <form> tag (I'll show you that later).

On the second line I declare and initialize all the variables that will contain the data on the form. You need one variable per input. They are initialized to the default value that you want on the form when it is first displayed.

If the form has been submitted I need to validate the data entered by the user. The code does the steps out of order like this so that I have a chance to re-display the form if invalid data is entered.

I set the variables to the data on the form by asking the Form collection for the values by the names I specified. Remember to always force the data to be the correct type by either adding a blank string, or subtracting zero as shown below:

var sName = ''  + Request.Form ( "name" );
var nNumber = Request.Form ( "age" ) - 0;

Then do the actual validation, and if it fails display a message and set bSubmitted=false to cause the form to be displayed again:

   // validate the name
   if ( !sName.length )
      Out ( 'You must enter a name<p>' );

      // pretend the form hasn't been sent yet
      bSubmitted = false;

Part 3: Displaying the form...

Featured sponsor
My favorite resources

I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...

Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!