Read data sent through a form

Source: Internet
Author: User
Tags count key variables query reset servervariables variable trim
Learning ASP, the most important thing is to master the ASP built-in six large objects. In fact, in the last lecture, we have already learned about the response object (yes?). There's no mistake! ), and the most commonly used write method, redirect method, and expires property in the response object. See the concepts of objects, methods, properties, collections, events (I don't know one!) If you have not contacted before, smart you do not care about these concepts, know how to use on the line, my view is just the beginning of the key lies in copying. Let's continue with the example to learn the request object, and in order to deepen our understanding, be sure to run these programs to see the output.

First, the use of request.servervariables to obtain environment variables, this part of the content is very simple, but the content is very important, how to get? Take a look at the following example:
<%@ Language=vbscript%>
<HTML><BODY>
<% ' wuf8.asp
Response.Write "Run ASP file path:" &_
Request.ServerVariables ("Script_name") & "<Br>"
Response.Write "Returns the data length of the 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 "returns the actual physical path to the home page:" &_
Request.ServerVariables ("Appl_physical_path") & "<Br>"
%>

<table colspan=8 cellpadding=5 border=0>
<tr>
&LT;TD align=center bgcolor= "#800000" width= "109" > <font style= "ARIAL narrow" color= "#ffffff" size= "2" > Environment variable Name </font></td>
&LT;TD align=center width=459 bgcolor= "#800000" > <font style= "ARIAL narrow" color= "#ffffff" size= "2" > Content </ Font></td>
</tr>
<tr>
&LT;TD bgcolor= "F7efde" align=center> <font style= "ARIAL Narrow" size= "2" >
Result1
</font></td>
&LT;TD bgcolor= "F7efde" align=center> <font style= "ARIAL Narrow" size= "2" >
Result2
</font></td></tr>
</table>
</BODY></HTML>
Now, you should find out how easy! the routine wuf2.asp in the last lecture
Note: The latter part of the program's HTML markup is purely for the following example, so don't be surprised. So, what other environment variables are there? Run the following example to know (this program removed part of the code, preferably to my site to download the source program easy to understand).
<%@ Language=vbscript%>
<% ' wuf9.asp
Option Explicit
Dim Sv
%>
<HTML><BODY>
<table colspan=8 cellpadding=5 border=0>
<tr>
&LT;TD align=center bgcolor= "#800000" width= "109" > <font style= "ARIAL narrow" color= "#ffffff" size= "2" > Environment variable Name </font></td>
&LT;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>
Here used for ... Each loop that enumerates all the elements in a collection. If the latter part does not understand, please compare wuf8.asp, and then look at the results of the operation, carefully understand (what attitude?) )。

Second, through the table one-way server transfer data (also can be understood, the server side how to read the data sent by the client)
Home page, you should know many home pages often use form forms to let users enter data and then send data through the Submit button. There are two main methods of "method" in the From form: Post and get, and "action" typically specifies a. CGI,. pl, or. asp file, and today we're going to learn to write this. asp file.
(i) If you use the Post method to transfer data, you use Request.Form to read the data.
First, edit the following wuf10.htm file for the user to enter data:
<body bgcolor= "#FFFFFF" >
<form method= "POST" action= "wuf11.asp" >
Name: <input type= "text" name= "Yourname" ><br>
Sex: <select name= "Gender" >
<option> male </option>
<option> Women </option>
</select> <br>
Message: <textarea name= "Messages" > Hello!
Pay attention to multiple lines of text processing </textarea> <br>
Hobbies (hold down the CTRL key to select more):
<select name= "hobby" multiple size= "4" >
<option> Computer </option>
<option> Shopping </option>
<option> movie </option>
<option> Reading </option>
</select> <br>
<input type= "Submit" name= "Submission" value= "submitted" >
<input type= "reset" name= "Submit2" value= "reset" >
</form>
</body>
Then write a wuf10.htm required file wuf11.asp 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 in addition to trailing spaces
Strgender = Trim (Request.Form ("gender"))
STRM = Trim (Request.Form ("message"))
STRMSG = Replace (Strm,vbcrlf, "<Br>" & vbCrLf)
' vbCrLf corresponds to a combination of carriage return and line feed. As for the Replace function, the function is to replace the vbCrLf in the string strm with "<Br>" & vbCrLf (Please think about this vbCrLf. See the output file HTML source code is clear), please refer to the VBScript Help in detail.
%>
<HTML><BODY>
Name: <%= strname%><br><br>
Sex: <%= 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 read the above, interested to see how to read multiple options
Response.Write "Select the number of Hobbies:" & 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 the sake of understanding, we use two programs, actually use only one program can also, if interested please see the following routine wuf12.asp, help deepen 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, this length will not be 0, so execute the following statement and display the results
' The following part is actually copied 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>
Sex: <%= strgender%><br><br>
Message: <Br><Br>
<%= strm%><br><br>
<%= strmsg%><br><br>

<%
Response.Write "Select the number of Hobbies:" & 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 first loaded, no data is submitted, so the previous section does not execute, but starts here
' That's why there are two reasons for <HTML></HTML>
' Copy wuf10.htm on the following lines
' <form method= ' post ' action= ' wuf11.asp ' is replaced with an environment variable, 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>
Sex: <select name= "Gender" >
<option> male </option>
<option> Women </option>
</select> <br>
Message: <textarea name= "Messages" > Hello!
Pay attention to multiple lines of text processing </textarea> <br>
Hobbies (hold down the CTRL key to select more):
<select name= "hobby" multiple size= "4" >
<option> Computer </option>
<option> Shopping </option>
<option> movie </option>
<option> Reading </option>
</select> <br>
<input type= "Submit" name= "Submission" value= "submitted" >
<input type= "reset" name= "Submit2" value= "reset" >
</form>
</BODY></HTML>
<%end if%>

(ii) If you use the Get method to transfer data, you use Request.QueryString to read the data.
First, edit the following wuf13.htm file for the user to enter data:
<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>
Sex: <select name= "Gender" >
<option> male </option>
<option> Women </option>
</select> <br>
<input type= "Submit" name= "Submission" value= "submitted" >
<input type= "reset" name= "Submit2" value= "reset" >
</form>
</body>
Then write a wuf13.htm required file wuf14.asp Collect data:
<%@ Language=vbscript%>
<% ' wuf14.asp
Option Explicit
Response.expires=0
Dim Strcname, Strename, Strgender

Strename = Trim (Request.QueryString ("ename")) ' trim function is used in addition to trailing spaces
Strcname = Trim (Request.QueryString ("Cname"))
Strgender = Trim (Request.QueryString ("gender"))
%>
<HTML><BODY>
English Name: <%= strename%><br><br>
Chinese name: <%= strcname%><br><br>
Sex: <%= strgender%><br><br>
Look at the submitted string: <Br>
<%= Request.ServerVariables ("query_string")%>
</BODY></HTML>
To better understand this program, you'd better try a routine wuf13.htm in your browser to see the output, and you'll find the long strings in the address bar déjà vu, just as you would see on Yahoo search. At this point, you try to enter the address bar directly "Http://localhost/wuf14.asp?Ename=Rose&Cname= Li Yi &gender= female", incredibly also got the same result. So you can see that the result of wuf13.htm is a link with a parameter like this. Request.QueryString, however, reads the individual data from the additional parameters of the http://address.
In fact, when the "submit" button is pressed, the query string (the input data) is appended to the URL address as a parameter (separated by "&" between the parameters) to pass the data. At the same time, note that the query string displayed in the browser does not have the Chinese language, but is not aware of the code containing the hundred-semicolon, this is because of the encoding. Finally, as before, these two programs can also be merged into a program (routine wuf15.asp, need to go to my site to download).

A few notes:
1. If the Post method is used to submit the data, the Request.ServerVariables ("Content_length") >0.
If you use the Get method to submit the data, Request.ServerVariables ("query_string") <> "".
2. After you understand the principle, you can mix Request.Form and request.querystring in the same ASP file.
3. If there are a few buttons on a form form, how do you determine which button the user pressed? If you look at it, you'll notice that in routine wuf11.asp, the value of the Submit button is also transferred, and the query string generated in wuf13.htm can finally find a similar value. Please note that only the value of the button is transmitted and the value of the other button is "", which is the basis of judgment (the InfoWeb website has a similar article).


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.