You are viewing a plain-jane printable version of http://CoverYourASP.com/Forms.asp.
See how this was done

 

Implementing forms with ASP

I think it's safe to say that every site needs a form - a contact or tell-a-friend form to send email, a Search form to search your database, or some sort of data entry form - probably all of these!

As you'll soon see, the framework for all these forms is the same. Once you've written one, the rest are easy - it's just copy and paste! The steps to follow are:

display form
      v
validate data
      v
process data
      v
[display form]

Below is the code that I use to implement this framework. Every form uses some variation of this - some don't need any validation, others always display the form again. But each form starts with the code below.

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" );

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

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

// show the form if not submitted yet
if ( !bSubmitted )
{
   Out ( 'Here\'s a simple form.' );

   Out ( '<form action="/FormPage.html" method="post">' );

      Out ( 'Name: ' );
      Out ( '<input type="text" name="name" size="10" value="' + sName + '">' );
      Out ( '<input type="submit" value="Display name">' );

   Out ( '</form>' );
}
else
{
   // process data
   Out ( 'Hi ' + sName );
}

Take a moment to glance over the code above, then I'll step you through this code, line-by-line, and examine what is happening.

Part 2: The framework...