ASP Lecture 2: Reading data sent through forms
Author:Eve Cole
Update Time:2009-05-30 19:58:19
The most important thing to learn ASP is to master the six built-in objects of ASP. In fact, in the previous lecture, we have already learned about the Response object (really? Did you make a mistake!), and the most commonly used Write method, Redirect method and Expires property in the Response object. When you see the concepts of objects, methods, properties, collections, and events (I don’t know any of them!), if you have never been exposed to them before, you are smart and don’t care about these concepts. Just know how to use them. My point of view is that it’s just the beginning. The key is to copy. Let's continue to learn the Request object through examples. In order to deepen our understanding, please run these programs to see the output results.
1. Use Request.ServerVariables to obtain environment variables. This part is very simple, but the obtained content is very important. How to obtain it? Please see the following example:
<%@ Language=VBScript %>
<HTML><BODY>
<% 'wuf8.asp
Response.Write "Path to run ASP file: " &_
Request.ServerVariables("Script_Name") & "<Br>"
Response.Write "Return the data length of content: " &_
Request.ServerVariables("Content_Length") & "<Br>"
Response.Write "Return the customer's IP address: " &_
Request.ServerVariables("Remote_Addr") & "<Br>"
Response.Write "Browser name: " &_
Request.ServerVariables("HTTP_USER_AGENT") & "<Br>"
Response.Write "Return to the actual physical path of the home page: " &_
Request.ServerVariables("APPL_PHYSICAL_PATH") & "<Br>"
%>
<table colspan=8 cellpadding=5 border=0>
<tr>
<td align=CENTER bgcolor="#800000" width="109"> <font style="ARIAL NARROW" color="#ffffff" size="2">Environment variable name</font></td>
<td align=CENTER width=459 bgcolor="#800000"> <font style="ARIAL NARROW" color="#ffffff" size="2">Content</font></td>
</tr>
<tr>
<td bgcolor="f7efde" align=CENTER> <font style="ARIAL NARROW" size="2">
result1
</font></td>
<td bgcolor="f7efde" align=CENTER> <font style="ARIAL NARROW" size="2">
result2
</font></td></tr>
</table>
</BODY></HTML>
Now, you should find how easy the wuf2.asp routine in the previous lecture turns out to be!
Note: The HTML markup in the latter part of this program is purely in preparation for the following example, so don't be surprised. So, what other environment variables are there? You will know by running the following example (this program has deleted some codes, it is best to download the source program from my site for easier understanding).
<%@ Language=VBScript %>
<% 'wuf9.asp
Option Explicit
Dim Sv
%>
<HTML><BODY>
<table colspan=8 cellpadding=5 border=0>
<tr>
<td align=CENTER bgcolor="#800000" width="109"> <font style="ARIAL NARROW" color="#ffffff" size="2">Environment variable name</font></td>
<td align=CENTER width=459 bgcolor="#800000"> <font style="ARIAL NARROW" color="#ffffff" size="2">Results</font></td>
</tr>
<%
for each Sv In Request.ServerVariables
Response.Write "<tr>"
Response.Write "<td bgcolor='f7efde' align=CENTER> <font style='ARIAL NARROW' size='2'>"
Response.Write Sv
Response.Write "</font></td>"
Response.Write "<td bgcolor='f7efde' align=CENTER> <font style='ARIAL NARROW' size='2'>"
Response.Write Request.ServerVariables(Sv)
Response.Write "</font></td></tr>"
next
%>
</table>
</BODY></HTML>
A For...Each loop is used here to list all elements in a collection. If you don't understand the second half, please compare wuf8.asp, take a look at the running results, and understand it carefully (what's your attitude?).
2. Transmit data to the server through the form (this can also be understood as how the server reads the data sent by the client)
If you have worked on a homepage, you should know that many homepages usually use Form forms to allow users to enter data, and then send the data through the "submit" button. The "method" in the From form has two main methods: POST and GET. After "action", a .cgi, .pl or .asp file is usually specified. What we are going to learn today is how to write this .asp file.
(1) If you use the POST method to transmit data, use Request.Form to read the data.
First edit the following wuf10.htm file for users to enter data:
<html>
<body bgcolor="#FFFFFF">
<form method="post" action="wuf11.asp">
Name: <input type="text" name="yourname"><br>
Gender: <select name="gender">
<option>Male</option>
<option>Female</option>
</select> <br>
Message: <textarea name="message">Hello!
Pay attention to the processing of multi-line text</textarea> <br>
Hobbies (hold down the Ctrl key to select multiple):
<select name="hobby" multiple size="4">
<option>Computer</option>
<option>Shopping</option>
<option>Movies</option>
<option>Reading</option>
</select> <br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>
</body>
</html>
Then write a file wuf11.asp that wuf10.htm needs to use to collect data:
<%@ Language=VBScript %>
<% 'wuf11.asp
Option Explicit
Response.Expires=0
Dim StrName, StrGender, StrM, StrMsg
StrName = Trim(Request.Form("yourname")) 'Trim function is used to remove leading and trailing spaces
StrGender = Trim(Request.Form("gender"))
StrM = Trim(Request.Form("message"))
StrMsg = Replace(StrM,vbcrlf,"<Br>" & vbcrlf)
' vbcrlf is equivalent to the combination of carriage return and line feed. As for the Replace function, its function is to replace vbcrlf in the string StrM with "<Br>" & vbcrlf (please think about the use of this vbcrlf? You will understand it by looking at the HTML source code of the output file). Please refer to the VBScript help for details.
%>
<HTML><BODY>
Name: <%= StrName%><Br><Br>
Gender: <%= StrGender%><Br><Br>
Message: <Br><Br>
<%= StrM%><Br><Br>
<%= StrMsg%><Br><Br>
In fact, the value of the "Submit" button is also passed: <Br>
<%= Request.Form("Submit")%><Br><Br>
<% 'First understand the above, and then see how to read multiple options if you are interested
Response.Write "Total number of hobbies selected:" & Request.Form("hobby").Count & "<Br>"
Dim I
For I = 1 to Request.Form("hobby").Count
Response.Write Request.Form("hobby")(I) & "<Br>"
Next
%>
</BODY></HTML>
In this example, for ease of understanding, we use two programs. In fact, you can also use only one program. If you are interested, please see the following routine wuf12.asp, which will help deepen your understanding of environment variables.
<%@ Language=VBScript %>
<% 'wuf12.asp
Option Explicit
Response.Expires=0
Dim StrName, StrGender, StrM, StrMsg
If Request.ServerVariables("Content_Length") <> 0 Then
'After submitting the data, the length will not be 0, so execute the following statement to display the result
'The following part is actually copied from wuf11.asp
StrName = Trim(Request.Form("yourname"))
StrGender = Trim(Request.Form("gender"))
StrM = Trim(Request.Form("message"))
StrMsg = Replace(StrM,vbcrlf,"<Br>" & vbcrlf)
%>
<HTML><BODY>
Name: <%= StrName%><Br><Br>
Gender: <%= StrGender%><Br><Br>
Message: <Br><Br>
<%= StrM%><Br><Br>
<%= StrMsg%><Br><Br>
<%
Response.Write "Total number of hobbies selected:" & Request.Form("hobby").Count & "<Br>"
Dim I
For I = 1 to Request.Form("hobby").Count
Response.Write Request.Form("hobby")(I) & "<Br>"
Next
%>
</BODY></HTML>
<%
Else
'When the page is loaded for the first time, no data is submitted, so the previous part is not executed, but starts from here
'This is why there are two pairs of <HTML></HTML>
'Just copy wuf10.htm below.
'<form method="post" action="wuf11.asp"> has been replaced with environment variables, but it is exactly the same
Response.Write "Look at the results: " & Request.ServerVariables("Script_name") & "<Br>"
%>
<HTML><BODY>
<form method="post" action="<%= Request.ServerVariables("Script_name")%>">
Name: <input type="text" name="yourname"><br>
Gender: <select name="gender">
<option>Male</option>
<option>Female</option>
</select> <br>
Message: <textarea name="message">Hello!
Pay attention to the processing of multi-line text</textarea> <br>
Hobbies (hold down the Ctrl key to select multiple):
<select name="hobby" multiple size="4">
<option>Computer</option>
<option>Shopping</option>
<option>Movies</option>
<option>Reading</option>
</select> <br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>
</BODY></HTML>
<%End If%>
(2) If you use the GET method to transmit data, use Request.Querystring to read the data.
First edit the following wuf13.htm file for users to enter data:
<html>
<body bgcolor="#FFFFFF">
<form method="get" action="wuf14.asp">
English name: <input type="text" name="Ename"> <br>
Chinese name: <input type="text" name="Cname"><br>
Gender: <select name="gender">
<option>Male</option>
<option>Female</option>
</select> <br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>
</body>
</html>
Then write a file wuf14.asp that wuf13.htm needs to use to collect data:
<%@ Language=VBScript %>
<% 'wuf14.asp
Option Explicit
Response.Expires=0
Dim StrCname, StrEname, StrGender
StrEname = Trim(Request.QueryString("Ename")) 'Trim function is used to remove leading and trailing spaces
StrCname = Trim(Request.QueryString("Cname"))
StrGender = Trim(Request.QueryString("gender"))
%>
<HTML><BODY>
English name: <%= StrEname%><Br><Br>
Chinese name: <%= StrCname%><Br><Br>
Gender: <%= StrGender%><Br><Br>
Take a look at the submitted string: <Br>
<%= Request.ServerVariables("Query_String")%>
</BODY></HTML>
In order to better understand this program, you'd better try the effect of the routine wuf13.htm in the browser first, and look at the output results. You will find that the long string in the address bar looks familiar, just like when searching on Yahoo Seen pretty much the same. At this time, you try to enter "http://localhost/wuf14.asp?Ename=Rose&Cname=李二&gender=female" directly in the address bar, and you get the same result. So you can see it this way, the result of wuf13.htm is to get a link with parameters like this. Request.QueryString reads each data from the additional parameters of the http:// address.
In fact, when the "Submit" button is pressed, the query string (input data) will be appended to the URL address in the form of parameters (each parameter is separated by "&") to achieve the purpose of transferring data. At the same time, please note that there is no Chinese in the query string displayed in the browser, but unrecognizable garbled characters containing percent signs. This is because it is encoded. Finally, as before, these two programs can also be merged into one program (routine wuf15.asp, you need to download it from my site).
A few notes:
1. If you use the POST method to submit data, Request.ServerVariables("Content_Length")>0.
If you use the GET method to submit data, then Request.ServerVariables("Query_String") <> "".
2. After understanding the principle, you can mix Request.Form and Request.QueryString in the same ASP file.
3. If there are several buttons in a Form, how do you determine which button the user pressed? If you pay attention, you will find that there is a sentence in the routine wuf11.asp that the value of the "Submit" button is also transmitted, and a similar value can be found in the query string generated in wuf13.htm. Please note: Only the value of the pressed button is transmitted, while the value of other buttons is "". This is the basis for judgment ("China Planning News" InfoWeb website has a similar article).