ASP and form work together

Source: Internet
Author: User

ASP and form work together

Last time we said that ASP aims to allow page interaction with users. How can we get user information? Of course, we use form, whether it is CGI, IDC, Java, or other technologies, to allow the browser users to input information through the form embedded in the WWW page. Then, form submits the data to the server, and the server sends the data to CGI or otherProgram. Since form is so important, let's take a look at how to obtain the data that users input in form in ASP. At the same time, we will also learn how ASP returns the results to the WWW page.

First, we create a form whose htmlCodeAs follows:

<HTML>

<Head> <title> order </title>

<Body>

<H2> order form example </H2>

<P>

Please provide us with the following information, and then click "Submit ":

<Form method = "Post" Action = "response. asp">

<P>

Last name: <input name = "fname" size = "48">

<P>

Name: <input name = "lname" size = "48">

<P>

Title: <input name = "title" type = radio value = ""> mr.

<Input name = "title" type = radio value = ""> ladies and gentlemen

<P> <input type = submit value = "Submit"> <input type = reset value = "clear">

</Form>

</Body>

</Html>

This form is a simple order example. It receives the customer's name and name, and then submits the data using the POST method. we know that form usually has two methods to submit data: Get and post. For data submitted by get, the WWW Server puts the data in the environment variable QUERY_STRING. For the POST method, the data is sent to the stdout of the WWW server, and CGI reads the data from its own stdin. traditional CGI methods must process the data themselves. however, in ASP, the idea of object-oriented is adopted, and all the data from the browser is encapsulated into the object request. the request has the form and querystring methods, which return the data submitted using the POST method and the get method respectively. we only need to use

Request. Form ("form domain name") or

Request. querystring ("query parameter name ")

You can.

Now, let's create the. asp file response. asp to process the data submitted in the preceding form. Here, we simply return the data submitted by the user to the user.

<HTML>

<Head>

<Body>

<%

Title = request. Form ("title ")

Lastname = request. Form ("lname ")

If title = "sir" then

%>

<% = Lastname %> sir

<% Elseif Title = "" then %>

<% = Lastname %> MS.

<% Else %>

<% = Request. Form ("fname") & "" & lastname %>

<% End if %>

</Body>

</Html>

Well, the above Code is hard to understand. Don't be afraid. After a while, you will say, it's easy. let's analyze it. first, the beginning and end of the string are the same as the ordinary HTML code. The key is the middle. if the delimiters <% and %> are omitted, and the equal sign of nothing is replaced with print, it becomes:

Title = request. Form ("title ")

Lastname = request. Form ("lname ")

If title = "sir" then

Mr. print lastname

Elseif Title = "" then

Ms. print lastname

Else

Print request. Form ("fname") & "& lastname

End if

Is this VB? Is it easy?

One thing to note is that ,. characters in ASP files that are not within the delimiter are interpreted as common HTML characters. as shown above, "Mr", "Ms ". ASP outputs are implemented by adding an equal sign and an object name (of course, ASP code is within the delimiter ). <% = lastname %>. in fact. in an ASP file, this form is used to remove any object from the delimiters: <% = Object Name %>.

As for if .. then to judge the branch, I don't need to explain it :)

Above we use <% = Object Name %> to implement ASP data output to the page, so that the. asp file looks closer to the HTML file. Below we use another method to achieve it:

<HTML>

<Head>

<Body>

<%

Title = request. Form ("title ")

Lastname = request. Form ("lname ")

If title = "sir" then

Response. Write lastname & "sir"

Elseif Title = "" then

Response. Write lastname & ""

Else

Response. Write Request. Form ("fname") & "& lastname

End if %>

</Body>

</Html>

Apart from the beginning and end, does this look more like a vbprogram? By the way, we use the write method of another ASP built-in object response to output data.

Which method should be used depends on your liking. However, if many format control commands are output as strings using write, it is really troublesome. I prefer the first method :)

We have learned how to process the data submitted using the POST method using ASP. How can we process the data submitted using the get method? In fact, the processing is the same, but the method for obtaining form data is different. For example, to obtain the value of "title", we use:

Request. querystring ("title ")

This method is also used to call the. asp file as follows:

<A href = "response. asp? Title = Mr & fname = Zhang & lname = 3 "> XXXXX </a>

(In fact, the form get method is finally converted into this form .)

You must pay attention to the method used for calling. ASP file, you should use the corresponding method to receive the query data. Otherwise, you are always reported that the query parameter is null, And I have encountered many times ^_^

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.