Beginning Active Server Pages

Forms

Now you know the basics of ASP, we'll take a look at another aspect of Active Server Pages - collecting data. In HTML, any page that has a textbox or other form of input has a Form tag - even a Search engine uses forms. Once the fields have been filled in by the user, the information is 'posted' to another location. This can be a CGI script, compiled DLL, ASP Page, or simply an email address in the form of a mailto: link.

Before we set about dealing with the data that is sent to an ASP page, we need to know how to collect it. So, we'll have a quick look at the HTML tags required for a Form. First, you need a form tag. This tells the browser where to post the data, and how to do it:

<form action="mypage.asp" method="POST">
</form>

(Note that all the other form fields must be in between these two tags in order to be posted to mypage.asp. Next, you need to specify some fields. This most often done with an input tag (there are a few exceptions, but we won't deal with them here - take a look at the options in your HTML editor). The tag takes the form

<input type="InputType" name="FieldName" value="InitialValue">

InputType is usually Text - a text box, or Hidden - a field which is not displayed on the page. FieldName is a unique ID for this field. When retrieving the data after it has been posted, you need to know this FieldName value. InitialValue is the value of the field before the user makes any input (such as the Text property of a VB textbox).

Finally, you need a button or image for the user to click on to submit the data. This is usually in the form

<input type="submit" value="Button Caption">

If you want to use an image as the button, use

<input type="image" src="PathtoImage">

For our example, we're going to collect information for becoming a new member of our fictional site. To do this we're going to create single ASP page to collect the information, and then process it. For accessing data

First, add the following code to an ASP page named forminput.asp. At the moment, all it does is collect the data.

 <form action="forminput.asp" method="POST">
  Your Name <input type="text" maxlength=20 name="Name"><br>
  Your Email <input type="text" maxlength=100 name="Email"><br>
  <input type="hidden" name="Posted" value="1">
  <input type="submit" value="Sign Up">
 </form>

As you can see, first we include a form tag. Next, we include two text boxes for the Name and Email. We have set the input fields maxlength property, so we don't get any users trying to enter anything longer than we want. Next, we have a hidden field called 'posted' (this is explained later), and finally a submit button with its caption set to "Sign Up".

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”