Asp reads post data sent by Form

Source: Internet
Author: User

The most important thing to learn about ASP is to master the six built-in ASP objects. In fact, we have learned about the Response object and the Write method, Redirect method, and Expires attributes that are most commonly used in the Response object. You can see the concepts of objects, methods, attributes, sets, and events (I don't know any of them !), If you have never touched on these concepts before, you just need to ignore these concepts and know how to use them. My point is that at the beginning, the key lies in copying. Next we will continue to learn the Request object through the instance. In order to deepen understanding, please run these programs to see the output results.

I. Using Request. ServerVariables to get environment variables is simple, but the obtained content is very important. How can I get it? See the following example:Copy codeThe Code is as follows: <% @ Language = VBScript %>
<HTML> <BODY>
<% 'Wuf8. asp
Response. Write "path for running ASP files :"&_
Request. ServerVariables ("Script_Name") & "<Br>"
Response. Write "Returned content Data Length :"&_
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 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 out how easy the previous example wuf2.asp was!
Note: the later part of the HTML mark of the program is purely prepared for the following example, so don't be surprised. So what other environment variables are there? Run the following example and you will know (this program deletes some code, so it is best to download the source program on my site for easy understanding ).Copy codeThe Code is as follows: <% @ 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"> result </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 we use... An Each loop is used to list all elements in a set. If the second half cannot be understood, check wuf8.asp and check the running result ?).

2. transmit data to the server through a form (you can also understand how the server reads the data sent by the client)
If you have made a homepage, you should know that many homepages usually use Form forms to allow users to input data, and then send data through the "submit (submit)" button. The "method" in the From form has two main methods: POST and GET, while the "action" usually specifies one. cgi ,. pl or. asp files. What we want to learn today is to write this. asp file.
(1) If the POST method is used to transmit data, Request. Form is used to read data.
Edit the following wuf10.htm file for users to enter data:Copy codeThe Code is as follows: <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!
Handle multiple lines of text </textarea> <br>
Hobbies (press Ctrl to select multiple options ):
<Select name = "holobby" multiple size = "4">
<Option> Computer </option>
<Option> shopping </option>
<Option> movie </option>
<Option> reading </option>
</Select> <br>
<Input type = "submit" name = "Submit" value = "submit">
<Input type = "reset" name = "Submit2" value = "Reset">
</Form>
</Body>
</Html>

Compile a wuf10.htm file wuf11.asp to collect data:Copy codeThe Code is as follows: <% @ 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 Spaces
StrGender = Trim (Request. Form ("gender "))
StrM = Trim (Request. Form ("message "))
StrMsg = Replace (StrM, vbcrlf, "<Br>" & vbcrlf)
'Vbcrlf is equivalent to a combination of carriage return and line feed. As for the Replace function, the function is to Replace vbcrlf In the StrM string with "<Br>" & vbcrlf (please think about the use of this vbcrlf? View the HTML source code of the output file.) For details, see the VBScript help.
%>
<HTML> <BODY>
Name: <% = StrName %> <Br>
Gender: <% = StrGender %> <Br>
Message: <Br>
<% = StrM %> <Br>
<% = StrMsg %> <Br>
In fact, the value of the "Submit" button is also passed: <Br>
<% = Request. Form ("Submit") %> <Br>
<% 'Read the above information first. If you are interested, see how to read multiple options.
Response. Write "Total number of hobbies:" & Request. Form ("holobby"). Count & "<Br>"
Dim I
For I = 1 to Request. Form ("holobby"). Count
Response. Write Request. Form ("holobby") (I) & "<Br>"
Next
%>
</BODY> </HTML>

In this example, we use two programs for ease of understanding. In fact, we can only use one program. If you are interested, please refer to the following routine wuf12.asp, this helps you better understand environment variables.Copy codeThe Code is as follows: <% @ Language = VBScript %>
<% 'Wuf12. asp
Option Explicit
Response. Expires = 0
Dim StrName, StrGender, StrM, StrMsg
If Request. ServerVariables ("Content_Length") <> 0 Then
'After the data is submitted, the length will not be 0. Therefore, execute the following statement to display the result.
'The following part is actually copying 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>
Gender: <% = StrGender %> <Br>
Message: <Br>
<% = StrM %> <Br>
<% = StrMsg %> <Br>
<%
Response. Write "Total number of hobbies:" & Request. Form ("holobby"). Count & "<Br>"
Dim I
For I = 1 to Request. Form ("holobby"). Count
Response. Write Request. Form ("holobby") (I) & "<Br>"
Next
%>
</BODY> </HTML>
<%
Else
'The first time the page was loaded, no data was submitted, so the previous part was not executed, but started from here.
'That's why there are two <HTML> </HTML> pairs.
'Copy wuf10.htm below.
'<Form method = "post" action = "wuf11.asp"> Replace the environment variable with the same one.
Response. Write "look at the result:" & 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!
Handle multiple lines of text </textarea> <br>
Hobbies (press Ctrl to select multiple options ):
<Select name = "holobby" multiple size = "4">
<Option> Computer </option>
<Option> shopping </option>
<Option> movie </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 the GET method is used to transmit data, Request. Querystring is used to read data.
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>
Compile a wuf13.htm file wuf14.asp 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 Spaces
StrCname = Trim (Request. QueryString ("Cname "))
StrGender = Trim (Request. QueryString ("gender "))
%>
<HTML> <BODY>
English name: <% = StrEname %> <Br>
Chinese name: <% = StrCname %> <Br>
Gender: <% = StrGender %> <Br>
Check the submitted string: <Br>
<% = Request. ServerVariables ("Query_String") %>
</BODY> </HTML>

To better understand this program, you 'd better test the effect of wuf13.htm in the browser first. Look at the output results and you will find that the long strings in the address bar seem familiar, as you can see during Yahoo search. In this case, you try to enter "http: // localhost/wuf14.asp?" in the address bar? Ename = Rose & Cname = & gender = female ", the same result is returned. As you can see, the result of wuf13.htm is a link similar to this with parameters. Request. QueryString reads data from the additional parameters of http: // address.

In fact, when you press the "Submit" button, the query string (input data) is appended to the URL address as a parameter (each parameter is separated ), to transfer data. At the same time, note that the query string displayed in the browser does not contain Chinese characters, but does not recognize garbage codes containing percent signs. This is because of encoding. Finally, as before, these two programs can also be combined into one program (routine wuf15.asp, which needs to be downloaded from my site ).
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, 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 presses? If you are interested, you will find that the value of the pay-as-you-go and pay-as-you-go instances is also transferred, and the query string generated in wuf13.htm can also find similar values. Note: Only the value of the pressed button is transferred, and the value of other buttons is "", which is the basis for judgment ("report" InfoWeb website has a similar article ).

Related 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.