| << 3.2.0- Using the Information Obtained From the User | Chapter3 | 3.2.2- Output to the User >> |
Capturing and Storing the Information
Do you recall that, in the first Try-it-Out using forms, we asked the user for their department using a text box named Department? Well, here it is again on the third line below:
<FORM ACTION="SpringRetreatNotice.asp" METHOD=POST>
Please type your department here:
<P><INPUT TYPE="TEXT" NAME="Department"></P>
<P><INPUT TYPE="RESET" VALUE="Reset data"></P>
<P><INPUT TYPE="SUBMIT" VALUE="Click here to send this information."></P>
</FORM>
When the user types their name and clicks on the Submit button the browser sends two pieces of information to the server. The first is a request that the server should get the file named SpringRetreatNotice.asp. The second is the list of the data that the user typed in to the field – in this case, the word Sales was typed into the field named Department.
Within SpringRetreatNotice.asp, we can then do as follows:
<%
Dim strDepartment
strDepartment = Request.Form("Department")
%>
So what does this do? Well, the first line tells the server to begin a section of ASP code. The second line dimensions (or declares) a variable called strDepartment. Then in the third line we set the contents of strDepartment to a value that ASP is holding in the Request object – the piece of information that the user entered into the form and submitted to this page. Once that information is saved within a variable, we can use it any way needed in our ASP code until the page requested has finished processing. We will see this used in our next Try-it-Out.
Getting the Syntax Right
The key here is to get the syntax correct. You must start with a line that creates the variable using the keyword Dim (which means 'dimension'). For now, keep your variable names very simple by only using letters, no numbers or symbols. Variable names can never contain spaces. To make it easier for you in this chapter, we begin all variables with the letters str (which is an abbreviation for 'string') since we are working with text.
Once the variable has been created with the Dim keyword, it can be filled with some information. The line that populates the variable must begin with the name of the variable. The variable name is followed by a space and the equals sign ( = ) and then another space. On the right of the equals sign, we must use the exact words Request.Form then open parenthesis, a double quote and the name of the input field from the page. Finish the line with another double quote and close the parenthesis. The right hand side of this "equation" instructs our ASP processor to look in the Form collection of the Request object; and from within that collection, to give us the data that has been assigned the name Department.
Don't worry about understanding the details behind the Request object and its Form collection just now – we'll be looking at both in Chapter 7 .
Working with Lots of Pieces of Information
You can create more than one variable at once using the Dim keyword, and separating the variable names with commas, like this:
Dim strLastName, strMiddleName, strFirstName
These variables are all of type Variant. We'll discuss what variants are in detail in Chapter 4 . For now, suffice it to say that a variant can hold any type of data – whether it be a string of text, or an integer, or whatever.
In our second Try-it-Out for forms, the request for information on the jackets, we would use the following code in the action page:
<%
Dim strGender, strSize, strColor
strGender = Request.Form("Gender")
strSize = Request.Form("Size")
strColor = Request.Form("Color")
%>
The variables strGender, strSize and strColor would be created (on the second line above) and then filled with the data from the Form collection of the Request object, which holds the data typed in by the user into those fields.
Common Errors
We will be using this technique in our next few Try-It-Outs, but before we use this, let's just take a quick look at some common sources of errors. The most common errors are in typos. If variable assignments don't work you should check:
- Exact spelling of the name that you gave to the input tag. It is also a good habit to stick to using the same case throughout, because some scripting languages (like JavaScript) are case sensitive – so a Rose is different from a rose is different from a ROSE (contrary to the wisdom of William Shakespeare!)
- Exact typing of the objects, methods and properties: Request.Form
- Keep your variable names very simple for starters: all letters, no symbols. Never use spaces in variable names
- The field name must be in double quotes, and that within parentheses
- Be sure to put the variable name first (to the left of the equals sign) and the source of data second (to the right of the equals sign)
- The assignment of the variable must be within ASP code delimiters (between <% and %>)
- Be sure that in your form page you added the attribute METHOD=POST in the <FORM> tag
Let us learn one more idea before we do the next Try-It-Out.
| << 3.2.0- Using the Information Obtained From the User | Chapter3 | 3.2.2- Output to the User >> |

RSS

