Databases and Cookies - Try It Out – Setting and Reading a Cookie with ASP Alone
(Page 5 of 13 )
Considering the dates of birth in our sailors list, it looks like we should accommodate folks that need glasses. Sailors trying to use laptops on yachts, where the bright sun makes reading a LCD screen difficult, exacerbate this problem. So we want to give sailors the ability to pick one of three types of displays: small, medium and large. Since this parameter will be utilized with every page, and is not sensitive, we can improve server speed by storing the setting in a cookie rather than in our People table for look–up.
You will have to create three pages. The first will be a "Set Preference" form to ask the sailor for her or his preference in type size. A second page will be a "Cookie Setter" that stores the preference in a cookie and offers a button to open the home page. The third is the "Home Page Using Preference" that will read the cookie and use that data to set its type style.
If you haven't worked with HTML styles before, I suggest you study the following few ideas:
- Style attributes are set for levels of text, for example <p>, <H1>, etc.
- Text for the remainder of the document will be formatted according to the style of its level
- Styles are set within the tags <STYLE> and </STYLE>
- Style tags are placed in the head section of the page
- Style attributes are set as Level {AttributeName: value} such as the following:
P {font–size: 14 pt}
- Multiple attributes are separated by semi colons as follows:
P {font–size: 14 pt; color: black}
You can get a much better feel for all the features of styles from Chapters 1 and 2 of Frank Boumphrey's book: Professional Style Sheets (ISBN 1-861001-65-7). This book also carries you through XML, an improved formatting technique.
The first page is a form that gets the font size preference from the user. The code for this page is available as 2726-11-Cook-TIO-01-SetPreferenceForm.htm:
<HTML><HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726 chapt11 Cookies TIO 01 SetPreferences</TITLE>
</HEAD><BODY>
Please select the size font you prefer.
<FORM ACTION="2726-11-Cook-TIO-01-CookieSetter.asp" METHOD=get id=form1 NAME=form1>
<INPUT TYPE="radio" NAME="PrefFontSize" VALUE=small> Small
<INPUT TYPE="radio" NAME="PrefFontSize" VALUE=medium CHECKED> Medium
<INPUT TYPE="radio" NAME="PrefFontSize" VALUE=large> Large<BR><BR>
<INPUT TYPE=submit>
</FORM>
</BODY></HTML>
The above code produces the following form page:
Then we need the response page that actually sets the cookie. 2726-11-Cook-TIO-01-CookieSetter.asp contains the following code:
<%@ Language=VBScript%>
<%Response.Buffer=true%>
<HTML><HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726 chapt11 Cookies TIO 01 Create Cookie that holds the Preferences</TITLE>
</HEAD><BODY>
<%
varPrefFontSize = Request.Querystring("PrefFontSize")
Response.Write "Your cookie holds a font size preference of " & varPrefFontSize & _ "<BR>"
Response.Cookies("PrefFontSize")=varPrefFontSize
Response.Cookies("PrefFontSize").Expires = date+365
%>
<A HREF="2726-11-Cook-TIO-01-HomePageUsingSizePreference.asp ">
Now that the font size is set, click here to go to the home page.</A>
</BODY></HTML>
The preceding cookie-setting code produces the screen below, if the visitor selected medium.
Now that the cookie is set we can use it in the following code for a home page, saved in file 2726-11-Cook-TIO-01-HomePageUsingPreference.asp:
<%@ Language=VBScript %>
<HTML><HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>2726 chapt11 Cookies TIO 01 HomePage Using the Font Size Preferences</TITLE>
</HEAD><BODY>
<%
'-------------------
' the plan to set sailor preference in view
' get cookie
' use sailor preference in Select Case to set styles
'-------------------
' get cookie
'
varPrefFontSize = Request.Cookies("PrefFontSize")
' next line for testing
' Response.Write "<HR>The cookie is " & varPrefFontSize & "<HR>"
'-------------------
' use sailor preference in Select Case to set styles
'
Response.Write "<STYLE>"
Select Case varPrefFontSize
case "large"
Response.Write "P {font-size: 26 pt}"
case "medium"
Response.Write "P {font-size: 16 pt}"
case "small"
Response.Write "P {font-size: 6 pt}"
End Select
Response.Write "</STYLE>"
%>
</HEAD>
<BODY>
<H2>Chapter 11 ASP-ADO and Cookies <BR><BR>
TIO #1 - Setting Page Styles According to Preferences</H2>
<P><P>Welcome to the Sailor Home Page.<BR><BR>
Your page is displayed as per your viewing preference</P>
</BODY></HTML>
The above home page code produces the screen below, again assuming that the medium size text was selected.
This is from Beginning ASP Databases by Kauffman, Spencer, and Willis (Apress, ISBN 1590592492). Check it out at your favorite bookstore today.
Buy this book now. |
Next: How it Works – Setting and Reading a Cookie with ASP Alone >>
More ASP.NET Articles
More By Apress Publishing