Open Directory Site ASP Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >> ASP >> ASP Request Object

 ASP Request Object

 

Request Object

When a browser asks for a page from a server, it is called a request. The Request Object is used to get information from the user.

Syntax

Request.collection

Request.property

Request.method

Collection

Collection Description

ClientCertificate Holds field values stored in the client certificate

Cookies(name) Holds cookie values

Form(element_name) Holds form (input) values. The form must use the post

method

QueryString(variable_name) Holds variable values in the query string

ServerVariables(server_variable) Holds server variable values

Properties

Property Description

TotalBytes Holds the total number of bytes the client is sending in

the body of the request

Method

Method Description

BinaryRead Fetches the data that is sent to the server from the client

as part of a post request

Examples

QueryString Collection Examples

1. Add extra query information to a Link

This example demonstrates how to send some extra query information to a page within a link, and retrieve that information on the destination page (which is, in this example, the same page).

Coding

<html>

<body>

<a href="demo_simplequerystring.asp?color=green">Example</a>

<%

Response.Write(Request.QueryString)

%>

</body>

</html>

Output

Example

2. A query string collection in its simplest use

This example demonstrates how the QueryString collection retrieves the values from a form. The form uses the GET method, which means that the information sent is visible to everybody (in the address field), and it limits the amount of information to send.

Coding

<html>

<body>

<form action="demo_simplereqquery.asp" method="get">

First name: <input type="text" name="fname"><br>

Last name: <input type="text" name="lname"><br>

<input type="submit" value="Submit">

</form>

<%

Response.Write(Request.QueryString)

%>

</body>

</html>

3. How to use informations for forms

This example demonstrates how to use the values retrieved from a form. We use the QueryString collection. The form uses the get method.

Coding

<html>

<body>

<form action="demo_reqquery.asp" method="get">

Please type your first name:

<input type="text" name="fname"><br><br>

<input type="submit" value="Submit">

</form>

<%

If Request.QueryString("fname")<>"" Then

Response.Write ("Hello " & Request.QueryString("fname") & "!")

Response.Write ("<br>How are you today?")

End If

%>

</body>

</html>

4. more information from a form

This example demonstrates what the QueryString contains if several input fields have the same name. It shows how to separate input fields with equal names from each other. It also shows how to use the Count keyword to count the "name" property. The form uses the get method.

Coding

<html>

<body>

<form action="demo_reqquery2.asp" method="get">

First name:

<input type="text" name="name" value="Donald">

<br>

Last name:

<input type="text" name="name" value="Duck">

<br>

<input type="submit" value="Submit">

</form>

<hr>

<p>The information received from the form above was:</p>

<%

If Request.QueryString("name")<>"" Then

Response.Write("<p>")

Response.Write("name=" & Request.QueryString("name"))

Response.Write("</p><p>")

Response.Write("The name property's count is: ")

Response.Write(Request.QueryString("name").Count)

Response.Write("</p><p>")

Response.Write("First name=" & Request.QueryString("name")(1))

Response.Write("</p><p>")

Response.Write("Last name=" & Request.QueryString("name")(2))

Response.Write("</p>")

end if

%>

</body>

</html>

Form Collection Examples

1. A form collection in its simplest use

This example demonstrates how the Form collection retrieves the values from a form. The form uses the POST method, which means that the information sent is invisible to others, and it has no limits (you can send a large amount of information).

Coding

<html>

<body>

<form action="demo_simpleform1.asp" method="post">

First name:

<input type="text" name="fname" value="Donald">

<br>

Last name:

<input type="text" name="lname" value="Duck">

<br>

<input type="submit" value="Submit">

</form>

<%

Response.Write(Request.Form)

%>

</body>

</html>

2. how to use informations form forms

This example demonstrates how to use the values retrieved from a form. We use the Form collection. The form uses the post method.

Coding

<html>

<body>

<form action="demo_simpleform.asp" method="post">

Please type your first name:

<input type="text" name="fname">

<br>

<input type="submit" value="Submit">

</form>

<%

If Request.Form("fname")<>"" Then

Response.Write ("Hello " & Request.Form("fname") & "!")

Response.Write ("<br>How are you today?")

End If

%>

</body>

</html>

3. more information form a form

This example demonstrates what the Form collection contains if several input fields have the same name. It shows how to separate input fields with equal names from each other. It also shows how to use the Count keyword to count the "name" property. The form uses the post method.

Coding

<html>

<body>

<form action="demo_form2.asp" method="post">

First name:

<input type="text" name="name" value="Donald">

<br>

Last name:

<input type="text" name="name" value="Duck">

<br>

<input type="submit" value="Submit">

</form>

<hr>

<p>The information received from the form above was:</p>

<%

If Request.Form("name")<>"" Then

Response.Write("<p>")

Response.Write("name=" & Request.Form("name"))

Response.Write("</p><p>")

Response.Write("The name property's count is: ")

Response.Write(Request.Form("name").Count)

Response.Write("</p><p>")

Response.Write("First name=" & Request.Form("name")(1))

Response.Write("</p><p>")

Response.Write("Last name=" & Request.Form("name")(2))

Response.Write("</p>")

End if

%>

</body>

</html>

4. a form with radio button

This example demonstrates how to interact with the user through radio buttons, with the Form collection. The form uses the post method.

Coding

<html>

<body>

<%

dim cars

cars=Request.Form("cars")

%>

<form action="demo_radiob.asp" method="post">

<p>Please select your favorite car:</p>

<input type="radio" name="cars" value="Volvo"

<%if cars = "Volvo" then Response.Write("checked")%>>

Volvo V70

<br>

<input type="radio" name="cars" value="Saab"

<%if cars = "Saab" then Response.Write("checked")%>>

Saab 95

<br>

<input type="submit" value="Submit">

</form>

<%

if cars<>"" then

Response.Write("<p>Your favorite car is: " & cars & "</p>")

end if

%>

</body>

</html>

5. a form with check box

This example demonstrates how to interact with the user through checkboxes, with the Form collection. The form uses the post method.

Coding

<html>

<body>

<%

fruits=Request.Form("fruits")

%>

<form action="demo_checkboxes.asp" method="post">

<p>Which of these fruits do you prefer:</p>

<input type="checkbox" name="fruits" value="Apples"

<%if instr(fruits,"Apple") then Response.Write("checked")%>>

Apple

<br>

<input type="checkbox" name="fruits" value="Oranges"

<%if instr(fruits,"Oranges") then Response.Write("checked")%>>

Orange

<br>

<input type="checkbox" name="fruits" value="Bananas"

<%if instr(fruits,"Banana") then Response.Write("checked")%>>

Banana

<br>

<input type="submit" value="Submit">

</form>

<%

if fruits<>"" then%>

<p>You like: <%Response.Write(fruits)%></p>

<%end if

%>

</body>

</html>

Other Examples

1. Server Variales

This example demonstrates how to find out the visitors (yours) browser type, IP address, and more with the ServerVariables collection.

Coding

<html>

<body>

<p>

<b>You are browsing this site with:</b>

<%Response.Write(Request.ServerVariables("http_user_agent"))%>

</p>

<p>

<b>Your IP address is:</b>

<%Response.Write(Request.ServerVariables("remote_addr"))%>

</p>

<p>

<b>The DNS lookup of the IP address is:</b>

<%Response.Write(Request.ServerVariables("remote_host"))%>

</p>

<p>

<b>The method used to call the page:</b>

<%Response.Write(Request.ServerVariables("request_method"))%>

</p>

<p>

<b>The server's domain name:</b>

<%Response.Write(Request.ServerVariables("server_name"))%>

</p>

<p>

<b>The server's port:</b>

<%Response.Write(Request.ServerVariables("server_port"))%>

</p>

<p>

<b>The server's software:</b>

<%Response.Write(Request.ServerVariables("server_software"))%>

</p>

</body>

</html>

Output

You are browsing this site with: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)

Your IP address is: 202.54.69.66

The DNS lookup of the IP address is: 202.54.69.66

The method used to call the page: GET

The server's domain name: www.w3schools.com

The server's port: 80

The server's software: Microsoft-IIS/4.0

2. Welcome Cookies

This example demonstrates how to create a Welcome Cookie with the Cookies Collection.

Coding

<%

response.cookies("NumVisits").Expires = date + 365

num=request.cookies("NumVisits")

If num = "" Then

response.cookies("NumVisits") = 1

Else

response.cookies("NumVisits") = num + 1

End If

%>

<html>

<body>

<%

if num="" then

%>

Welcome! This is first time you are visiting this Web page.

<%

else

%>

You have visited this Web page <%response.write(num)%> times before

<%

end if

%>

</body>

</html>

Output

Welcome! This is first time you are visiting this Web page.

3. Total Bytes

This example demonstrates how to use the TotalBytes property to find out the total number of bytes the user sent in the Request object.

Coding

<html>

<body>

<form action="demo_totalbytes.asp" method="post">

Please type something:

<input type="text" name="txt"><br><br>

<input type="submit" value="Submit">

</form>

<%

If Request.Form("txt")<>"" Then

Response.Write("<p>You typed: ")

Response.Write(Request.Form("txt") & ".</p>")

Response.Write("<p>The user sent: ")

Response.Write(Request.Totalbytes & " bytes.</p>")

End If

%>

</body>

</html>

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting