If your application deals with browsers that do not support cookies, you will have to use other methods to pass information from one page to another in your application. There are two ways of doing this:
1. Add parameters to a URL
You can add parameters to a URL:
Go to Welcome Page
And retrieve the values in the "welcome.asp" file like this:
<% fname=Request.querystring("fname") lname=Request.querystring("lname") response.write("
Hello " & fname & " " & lname & "!
")
response.write("
Welcome to my Web site!
")
%>
2. Use a form
You can use a form. The form passes the user input to "welcome.asp" when the user clicks on the Submit button:
First Name:
Last Name:
Retrieve the values in the "welcome.asp" file like this:
<% fname=Request.form("fname") lname=Request.form("lname") response.write("
Hello " & fname & " " & lname & "!
")
response.write("
Welcome to my Web site!
")
%>
8.ASP Session Object
A Session object stores information about, or change settings for a user session.
Monday, March 22, 2010
Read all Cookies
Look at the following code:
<% Response.Cookies("firstname")="Alex" Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Smith" Response.Cookies("user")("country")="Norway" Response.Cookies("user")("age")="25" %>
Assume that your server has sent all the cookies above to a user.
Now we want to read all the cookies sent to a user. The example below shows how to do it (note that the code below checks if a cookie has Keys with the HasKeys property):
<% dim x,y for each x in Request.Cookies response.write(" ") if Request.Cookies(x).HasKeys then for each y in Request.Cookies(x) response.write(x & ":" & y & "=" & Request.Cookies(x)(y)) response.write(" ") next else Response.Write(x & "=" & Request.Cookies(x) & " ") end if response.write " " next %>
Output:
firstname=Alex
user:firstname=John
user:lastname=Smith
user:country=Norway
user:age=25
<% Response.Cookies("firstname")="Alex" Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Smith" Response.Cookies("user")("country")="Norway" Response.Cookies("user")("age")="25" %>
Assume that your server has sent all the cookies above to a user.
Now we want to read all the cookies sent to a user. The example below shows how to do it (note that the code below checks if a cookie has Keys with the HasKeys property):
<% dim x,y for each x in Request.Cookies response.write(" ") if Request.Cookies(x).HasKeys then for each y in Request.Cookies(x) response.write(x & ":" & y & "=" & Request.Cookies(x)(y)) response.write(" ") next else Response.Write(x & "=" & Request.Cookies(x) & " ") end if response.write " " next %>
Output:
firstname=Alex
user:firstname=John
user:lastname=Smith
user:country=Norway
user:age=25
A Cookie with Keys
If a cookie contains a collection of multiple values, we say that the cookie has Keys.
In the example below, we will create a cookie collection named "user". The "user" cookie has Keys that contains information about a user:
<% Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Smith" Response.Cookies("user")("country")="Norway" Response.Cookies("user")("age")="25" %>
In the example below, we will create a cookie collection named "user". The "user" cookie has Keys that contains information about a user:
<% Response.Cookies("user")("firstname")="John" Response.Cookies("user")("lastname")="Smith" Response.Cookies("user")("country")="Norway" Response.Cookies("user")("age")="25" %>
How to Retrieve a Cookie Value?
The "Request.Cookies" command is used to retrieve a cookie value.
In the example below, we retrieve the value of the cookie named "firstname" and display it on a page:
<% fname=Request.Cookies("firstname") response.write("Firstname=" & fname) %>
Output: Firstname=Alex
In the example below, we retrieve the value of the cookie named "firstname" and display it on a page:
<% fname=Request.Cookies("firstname") response.write("Firstname=" & fname) %>
Output: Firstname=Alex
How to Create a Cookie?
The "Response.Cookies" command is used to create cookies.
Note: The Response.Cookies command must appear BEFORE the tag.
In the example below, we will create a cookie named "firstname" and assign the value "Alex" to it:
<% Response.Cookies("firstname")="Alex" %>
It is also possible to assign properties to a cookie, like setting a date when the cookie should expire:
<% Response.Cookies("firstname")="Alex" Response.Cookies("firstname").Expires=#May 10,2012# %>
Note: The Response.Cookies command must appear BEFORE the tag.
In the example below, we will create a cookie named "firstname" and assign the value "Alex" to it:
<% Response.Cookies("firstname")="Alex" %>
It is also possible to assign properties to a cookie, like setting a date when the cookie should expire:
<% Response.Cookies("firstname")="Alex" Response.Cookies("firstname").Expires=#May 10,2012# %>
What is a Cookie?
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both create and retrieve cookie values.
Form Validation in active server pages
User input should be validated on the browser whenever possible (by client scripts). Browser validation is faster and reduces the server load.
You should consider server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.
7 ASP Cookies
You should consider server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.
7 ASP Cookies
A form with radio buttons using asp
How to interact with the user, through radio buttons, with the Request.Form command.
<% dim cars cars=Request.Form("cars") %>
Please select your favorite car:
<%if cars="Volvo" then Response.Write("checked")%>
value="Volvo">Volvo
<%if cars="Saab" then Response.Write("checked")%>
value="Saab">Saab
<%if cars="BMW" then Response.Write("checked")%>
value="BMW">BMW
<% if cars<>"" then
Response.Write("
Your favorite car is: " & cars & "
")
end if
%>
<% dim cars cars=Request.Form("cars") %>
Please select your favorite car:
<%if cars="Volvo" then Response.Write("checked")%>
value="Volvo">Volvo
<%if cars="Saab" then Response.Write("checked")%>
value="Saab">Saab
<%if cars="BMW" then Response.Write("checked")%>
value="BMW">BMW
<% if cars<>"" then
Response.Write("
Your favorite car is: " & cars & "
")
end if
%>
Request.Form in asp
The Request.Form command is used to collect values in a form with method="post".
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
If a user typed "Bill" and "Gates" in the HTML form above, the URL sent to the server would look like this:
http://www.w3schools.com/simpleform.asp
Assume that "simpleform.asp" contains the following ASP script:
Welcome
<% response.write(request.form("fname")) response.write(" " & request.form("lname")) %>
The browser will display the following in the body of the document:
Welcome Bill Gates
Example:
A form with method="post"
How to interact with the user, with the Request.Form command.
Your name:
<% dim fname fname=Request.Form("fname") If fname<>"" Then
Response.Write("Hello " & fname & "!
")
Response.Write("How are you today?")
End If
%>
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
If a user typed "Bill" and "Gates" in the HTML form above, the URL sent to the server would look like this:
http://www.w3schools.com/simpleform.asp
Assume that "simpleform.asp" contains the following ASP script:
Welcome
<% response.write(request.form("fname")) response.write(" " & request.form("lname")) %>
The browser will display the following in the body of the document:
Welcome Bill Gates
Example:
A form with method="post"
How to interact with the user, with the Request.Form command.
Your name:
<% dim fname fname=Request.Form("fname") If fname<>"" Then
Response.Write("Hello " & fname & "!
")
Response.Write("How are you today?")
End If
%>
Request.QueryString in asp
The Request.QueryString command is used to collect values in a form with method="get".
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
If a user typed "Bill" and "Gates" in the HTML form above, the URL sent to the server would look like this:
http://www.w3schools.com/simpleform.asp?fname=Bill&lname=Gates
Assume that "simpleform.asp" contains the following ASP script:
Welcome
<% response.write(request.querystring("fname")) response.write(" " & request.querystring("lname")) %>
The browser will display the following in the body of the document:
Welcome Bill Gates
Examples
A form with method="get"
How to interact with the user, with the Request.QueryString command.
Your name:
<% dim fname fname=Request.QueryString("fname") If fname<>"" Then
Response.Write("Hello " & fname & "!
")
Response.Write("How are you today?")
End If
%>
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
If a user typed "Bill" and "Gates" in the HTML form above, the URL sent to the server would look like this:
http://www.w3schools.com/simpleform.asp?fname=Bill&lname=Gates
Assume that "simpleform.asp" contains the following ASP script:
Welcome
<% response.write(request.querystring("fname")) response.write(" " & request.querystring("lname")) %>
The browser will display the following in the body of the document:
Welcome Bill Gates
Examples
A form with method="get"
How to interact with the user, with the Request.QueryString command.
Your name:
<% dim fname fname=Request.QueryString("fname") If fname<>"" Then
Response.Write("Hello " & fname & "!
")
Response.Write("How are you today?")
End If
%>
Subscribe to:
Posts (Atom)