All values provided when the user fills in the page <FORM> content, or enters the value after the URL in the browser address bar, is used by the ASP script through the Form and QueryString collections. This is a simple way to access values in ASP code.
1. General techniques for accessing ASP collections.
Most ASP collections are similar to ordinary collections seen in VB. Effectively, they are arrays of values, but are accessed using a text string key (which is not size-sensitive) and an integer index. Therefore, if the client Web page contains the <FORM> as follows:
<FORM ACTION="show_request.asp" METHOD="POST">
FirstName:<INPUT TYPE=”TEXT” NAME=”FirstName”>
LastName:<INPUT TYPE=”TEXT” NAME=”LastName”>
<INPUT TYPE=”SUBMIT” VALUE=”Send”>
</FORM>
You can access the value in its control by accessing ASP's Form collection:
strFirstName = Request.Form("FirstName")
strLastName = Request.Form("LastName")
can also use the integer index of the controls in the form. The range of the index starts from the first control defined in the HTML, and then sorts according to the order of definition:
strFirstName = Request.Form( 1)
strLastName = Request.Form(2)
However, the latter technique of using integer indexing is not recommended, because once the control in the HTML changes, or a new control is inserted, the ASP code will get the wrong value. Furthermore, it is extremely confusing for people reading the code.
1) To access all values of the collection,
you can turn a series of values on the entire Form into a single character variable by referencing the collection without providing a key or index.
StrAllFormContent = Request.Form
If the text box contains the values Priscilla and Descartes, the Request.Form statement will return the following characters:
FirstName=Priscilla&LastName=Descartes
Note that the values provided appear in the form of name/value pairs (i.e. Control Name = Control value), and each name/value pair is separated from each other by the symbol "&". This technique is useful if you plan to pass the contents of the form to a separate executable application or DLL that expects the values to be in a standard format. Generally speaking, however, the contents of a collection are accessed by using the name of the control on the form as a text key.
2) Traversing an ASP collection
There are two ways to traverse all members in an ASP collection. The methods are basically the same as ordinary VB collections. Each collection provides a Count property, which returns the number of items in the collection. It can be traversed using the Count property using an integer index.
For intLoop=1 To Request.Form.Count
Response.Write Request.Form(intLoop) & “<BR>”
NextIf
the previous form contains two text boxes with Priscilla and Descartes values, you will get the following results:
Priscilla
Descartes
However, a better approach is to use the For Each...Next structure.
For Each objItem In Request.Form
Response.Write objItem & “=" & Request.Form(objItem) & “<BR>”
NextThe
benefit of this is that you can access both the name and the value of the control. The above code will result in the following:
FirstName = Priscilla
LastName = Descartes
Note that the <FORM> values returned by some browsers to ASP may not be the same as the order displayed on the page.
3) Multi-value nature of collection members
In some cases, each member in the ASP collection may have more than one value. This happens when there are several controls in the HTML definition that have the same Name attribute. For example:
<FORM ACTION=”Show_request.asp” METHOD=”POST”>
<INPUT TYPE=”TEXT” NAME=”OtherHobby”>
<INPUT TYPE=”TEXT” NAME=”OtherHobby”>
<INPUT TYPE=”TEXT” NAME=”OtherHobby”>
<INPUT TYPE=”SUBMIT” VALUE=”Send”>
</FORM>
In the Form collection, an entry will be created for "OtherHobby". However, it will include the values from the three text boxes. If the user leaves one or more fields blank when submitting, the returned value is an empty string. If the user enters Gardening and Mountaineering in the first and third text boxes respectively, and the second text box is empty, accessing Request.Form("OtherHobby") in our ASP code will return the string:
Gardening, ,Mountaineering
To be able to access a single value in this case, you can use more complex code:
For Each objItem In Request.Form
If Request.Form(objItem).Count >1 Then 'More than one value in this item Response.Write objItem & “:<BR>”
For intLoop = 1 To Request.Form(objItem).Count
Response.Write “Subkey” & intLoop & “value = “& Request.Form(objItem) (intLoop) & “<BR>”
Next
Else
Response.Write objItem & “ = ” & Request.Form(objItem) & “<BR>”
End If
NextFor
the previous form instance containing three OtherHobby controls, this would return:
OtherHobby:
Subkey 1 value = Gardening
Subkey 2 value =
Subkey 3 value = Mountaineering
However, since it is rare to give multiple text boxes the same name, this technique is rarely used.
a) Radio or page button controls in HTML
In HTML, the situation where several controls need to be given the same Name attribute is the radio (or option) button, for example:
<FORM ACTION="show_request.asp" METHOD=" POST”>
I live in:
<INPUT TYPE=”RADIO” NAME=”Country” VALUE=”AM”>America<BR>
<INPUT TYPE=”RADIO” NAME=”Country” VALUE=”EU”>Europe<BR>
<INPUT TYPE=”RADIO” NAME=”Country” VALUE=”AS”>Asia<BR>
<INPUT TYPE=”SUBMIT” VALUE=”Send”>
</FORM>
Because the user can only select one of the multiple items (which is why they are given the same name), only one return value will be obtained, and the browser can only send the value of the selected control. Therefore, if the user of this form has selected "Europez", this entry will be obtained, and its value will be obtained by traversing the Form set:
Country = EU
Since a different VALUE attribute is provided for each control, it reflects the value corresponding to each entry. The name of the country or region. If the VALUE attribute is omitted, the browser will return the value "on", so you will get:
Country = on
This is not often used, so the VALUE attribute is generally used for radio controls with the same name.
b) HTML check box control
When the HTML source code in a form contains a check box control, it is generally given a unique name, for example:
<FORM ACTION="show_request.asp" METHOD="POST">
I enjoy:
<INPUT TYPE=”CHECKBOX” NAME=”Reading” CHECKED> Reading
<INPUT TYPE=”CHECKBOX” NAME=”Eating”>Eating
<INPUT TYPE=”CHECKBOX” NAME=”Sleeping”> Sleeping
<INPUT TYPE=”SUBMIT” VALUE=”Send”>
</FORM>
In this case, when submitting the form, if only the first and third check boxes are selected (marked), when traversing the Form collection, the following values will be obtained:
Reading = on
Sleeping = on
However, if you provide a value for each checkbox, this value is sent to the server instead of the string "on". For example, the form is as follows:
<FORM ACTION="show_request.asp" METHOD="POST">
I enjoy:
<INPUT TYPE=”CHECKBOX” VALUE=”Hobby025” NAME=”Hobby” CHECKED>_
Swimming
<INPUT TYPE=”CHECKBOX” VALUE=”Hobby003” NAME=”Hobby” CHECKED>_
Reading
<INPUT TYPE=”CHECKBOX” VALUE=”Hobby068” NAME=”Hobby”>Eating
<INPUT TYPE=”CHECKBOX” VALUE=”Hobby010” NAME=”Hobby”>Sleeping
<INPUT TYPE=”SUBMIT” VALUE=”Send”>
</FORM>
If all but the third check box are submitted, the Request.Form collection will produce the following results:
Hobby = Hobby025, Hobby003, Hobby010
If you write more complex collection traversal code, as mentioned previously (shown separately each subkey), you get the result:
Hobby:
Subkey 1 value = Hobby025
Subkey 2 value = Hobby003
Subkey 3 value = Hobby010
It should be noted that in two cases, the unselected control does not return any value at all. In the result of the first case, there are no deceptive commas, and there are no null values in the second case. This is different from the results of the equivalent test using text boxes described above. When using text boxes, each text box returns a value, even an empty string. This is caused by the browser. Therefore, you should pay attention to this issue when accessing collections in ASP code.
A tricky side effect of the above situation is that when using checkboxes, the index of the checkbox value has no relationship to the position of the control in the original HTML. In the above example, the fourth checkbox has a subkey number of 3. , because when the form is submitted, the second control is not selected.
c) HTML list control
The <SELECT> tag in HTML is used to generate a standard drop-down list box, whose values are represented in an interesting hybrid way. The following form is created with 5 values for the user to select from. Since it contains the MULTIPLE attribute, it is possible to select more than one entry by holding down the Shift or Ctrl key while selecting.
<FORM ACTION=”show_request.asp” METHOD=”POST”>
<SELECT NAME=”Hobby” SIZE=”5” MULTIPLE>
<OPTION VALUE=”Hobby001”>Programming</OPTION>
<OPTION VALUE=”Hobby025”>Swimming</OPTION>
<OPTION VALUE=”Hobby003”>Reading</OPTION>
<OPTION VALUE=”Hobby068”>Eating</OPTION>
<OPTION VALUE=”Hobby010”>Sleeping</OPTION>
</SELECT><P>
<INPUT TYPE=”SUBMIT” VALUE=”Send”>
</FORM>
returns a single entry in the Form collection, which contains the selected values (the VALUE attribute specified in a single <OPTION> tag), separated by commas:
Hobby = Hobby025, Hobby003, Hobby010
if Using a slightly more complex set traversal code (showing each subkey individually), we get:
Hobby:
Subkey 1 value = Hobby025
Subkey 2 value = Hobby003
Subkey 3 value = Hobby010
This is the same situation as above for the checkbox with the same name. In fact, a SELECT list can be thought of as a list of check boxes for selecting (not checking) the corresponding items.
However, the list box also has a specified value. If you set the VALUE attribute in the <OPTION> tag, you will get the text content of the selected option. The Request.Form set will contain such an item:
Hobby = Swimming, Reading, Sleeping
and, Likewise, more complex collection iteration code would return results like this:
Hobby:
Subkey 1 value = Swimming
Subkey 2 value = Reading
Subkey 3 value = Sleeping
Of course, if a single item is selected and the VALUE attribute is provided in <OPTION>, the result will only contain:
Hobby = Hobby025.
If the VALUE attribute is not provided, the result will be:
Hobby = Swimming.
This allows it to be missing. Provincial (that is, no VALUE) displays the option text and can also make corresponding changes. The latter case is extremely useful in certain situations, such as when you want to display (a descriptive string) and pass a completely different content (such as using a short code to represent a descriptive string).
d) HTML submission and image controls
checkboxes and radio buttons are examples of boolean controls that return "on" when checked or selected. Unlike text boxes and most other HTML controls, browsers do not include unchecked or selected controls. The value of the control is not selected.
There is another commonly used Boolean control called HTML button. Such as <INPUT TYPE="SUBMIT">, <INPUT TYPE="RESET">, <INPUT TYPE="IMAGE">, <INPUT TYPE="BUTTON"> and <BUTTON>...</BUTTON> types.
BUTTON type controls do not return any value because they have no direct impact on the form. Even if you use the Submit method used to call the form, the browser will not include the value of the BUTTON type control in any request. Likewise, the value of an <INPUT TYPE="RESET"> button is never sent to the server.
However, input button controls of type SUBMIT and IMAGE actually submit the form to the server, and their VALUE properties contain the values of the form's other controls (as long as a NAME attribute is included in the HTML definition). For example, this form might be part of a wizard-type Web application that allows the user to step through or cancel a process:
<FORM ACTION="show_request.asp" METHOD="POST">
<INPUT TYPE=”SUBMIT” NAME=”btnSubmit” VALUE=”Next”>
<INPUT TYPE=”SUBMIT” NAME=”btnSubmit” VALUE=”Previous”>
<INPUT TYPE=”SUBIMT” NAME=”btnSubmit” VALUE=”Cancel”>
</FORM>
In a form, you can include multiple SUBMIT buttons. In this case, each button should be given a unique VALUE attribute, as shown above. When a form is submitted, traversing the values of the Request.Form collection will produce a value that depends on which button was pressed to submit the form. If the user presses the "Previous" button, he will get:
btnSubmit = Previous
Therefore, the Request.Form collection can be queried to determine the next displayed page, for example:
Select Case Request.Form("btnSubmit")
Case "Next"
Response.Redirect "page_3.asp"
Case "Previous"
Response.Redirect "page_1.asp"
Case "Cancel"
Response.Redirect "main_menu.asp"
End Select
At the same time, you can also use different NAME attributes for each button as needed. And select the control name whose value is contained in the Form collection. Extremely useful in situations where the control does not have a complete markup but is instead followed by a longer text label, as shown in the image below.
The interface on this screen is generated by the following code:
<FORM ACTION="show_request.asp" METHOD="POST">
<B>What do you want to do now?</B><P>
<INPUT TYPE=”SUBMIT” NAME=”btnNext” VALUE= ”>Go on the next page<P>
<INPUT TYPE=”SUBMIT” NAME=”btnPrevious” Value=” ”> GO back to the previous page<P>
<INPUT TYPE=”SUBMIT” NAME=”btnCancel” VALUE=” ”>Cancel and go back to the main menu page<P>
</FORM>
In the ASP page, after receiving the data, you can check the value provided by the button name to determine which button was pressed.
If Len(Request.Form(“btnNext”)) Then Response.Redirect “page_3.asp”
If Len(Request.Form(“btnPrevious”)) Then Response.Redirect “page_1.asp”
If Len(Request.Form("btnCancel")) Then Response.Redirect "main_menu.asp"
This job is to query the ASP collection on a key and return an empty string if it does not exist. In other words, if the second button (previous page) is pressed, the value of Request.Form("btnNext") is an empty string, and its length is zero without generating an error. When the second button is pressed, the value of this entry in the Form collection, Request.Form("btnPrevious"), will be " " and its length is greater than zero.
e) Improve the efficiency of using the Request collection.
Accessing an ASP collection to download a value is a time-consuming and computationally intensive process because the operation involves a series of searches of related collections, which is much slower than accessing a local variable. Therefore, if you plan to use a value in the collection multiple times on the page, you should consider storing it as a local variable, for example:
strTitle = Request.Form("Title")
strFirstName = Request.Form("FirstName")
strLastName = Request.Form("LastName")
If Len(stTitle) Then strTitle = strTitle & “ “
If strFirstName = " " Then
StrFullName = strTitle & " " & strLastName
ElseIf Len(strFirstName) = 1 Then
StrFullName = strTitle & strFirstName & “· “ & strLastName
Else
StrFullName = strTitle & strFirstName & " " & strLastName
End If
f) Search all Request collections
In some cases, it is possible to know that the key name of a value will appear in the Request collection, but not exactly which collection it is. For example, if several pages (or different sections of a page) send a value to the same ASP script, it may appear in the Form or QueryString collection.
To see why a value may appear in different collections, consider this situation: a page is requested using the <A> hyperlink element. In this case, the only way to add a value to the request is to add it to the URL. However, the same value may already appear in a <FORM> on another page, or in a different part of the same page:
...
<FORM ACTION=”process_page.asp” METHOD=”POST”>
<INPUT TYPE=”SUBMIT” NAME=”page” VALUE=”Next”>
<INPUT TYPE=”SUBMIT” NAME=”page” VALUE=”Previous”>
<INPUT TYPE=”SUBMIT” NAME=”page” VALUE=”Help”>
</FORM>
...
...
For help go to the <A HREF=”process_page.asp?page=Help”>Help Page</A>
...
In this case, pressing the Help button on the form will send the name/value pair "page=Help" from the Request.Form collection. However, pressing the <A> hyperlink may also send the name/value "Page=Help", but this time in the QueryString collection. To access this value, use a special feature of the ASP Request object:
strPage = Request("page")
This will search the entire collection - QueryString, Form, Cookies, ClientCertificate, ServerVariables - in order, until the name of the first matching value is found. Doing so is less efficient than accessing the appropriate collection directly, and is unsafe unless there is absolute guarantee that the value will not appear in another collection.
For example, you might want to collect the name of the Web server that fulfills a client's request by looking for "SERVER_NAME" in the Request.ServerVariables collection that appears in every query. However, if any other collection also contains a value named "server_name" (remember that key names are not case-sensitive), you will get incorrect results when using Request("server_name"). Using the Reqeust.ServerVariables("server_name") syntax, we will have a difficult time tracking errors.
In summary, use the "search all collection" technique with extreme caution and only when no other technique will provide the results you need.
g) Access other collections
In this section of this article, we have focused on the Form collection, which is probably the most used one. However, all these techniques are equally applicable to other objects. Including those provided by the Request object (i.e. Form, QueryString, Cookies, ServerVariables and ClientCertificate) collections, and the cookies (and collections provided by other objects) provided by the Response object.
We'll take a brief look at how a value is entered into a QueryString collection, along with its advantages and disadvantages. However, both cookie sets have additional functionality that makes using cookies more convenient, as discussed below.