The <form> frameworkvar 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... |