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".
Comments