Programmer to Programmer (TM) |
![]() | ||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||
Links Author Page About ASPToday |
![]() | |
Home | Today's Article | Search | Feedback | Write For Us | Suggest an Article | Advertise |
Monday, January 03, 2000 |
![]() |
By David Schultz |
ASP Tricks |
enter the discussion |
Is giving users important information quickly, or displaying a quick tip on site usage is just what your site needs? I'm going to show you how to do this here. I'm going to use ASP to create a "tip of the day" popup window pretty similar to that of Microsoft Office or Windows 95, that you'll easily able to import to your site.
I'm going to develop a very simple site that produces a unique pop up window every day when the user opens the initial page. I'll also build a basic administration page that allows you to add, delete, update and reset the tips.
I used Windows 95, Internet Explorer 5.0, Visual Interdev 6.0, HomeSite 4.0 and Access 97 to create this program, which requires that client has a JavaScript supporting browser. So to the workings:
A single Access 97 table holds all of the tips. This is the table layout I used:
Tips | ||
---|---|---|
Column Name | Data Type | Description |
ID | Auto Number | Used to uniquely identify each tip. |
Tip_Date | Date/Time | Date the tip was/will be displayed |
Tip_Text | Text | The actual text that is to be displayed |
Although you can use almost any storage technique (text file, other databases, etc) for the tips, I chose Access because it works well in my development environment. For those in a production environment I'd recommend something like SQL Server to host the tips database.
The Tip_Date column is interesting because it is used several different ways. First it is used to determine if the tip has been used before, then it allows you to assign a particular tip to a particular day. So you might use this for a holiday greeting message, or reminders on certain days of the year.
I'm going to got through the file and describe what each does, highlighting the more interesting code
The global.asa
file is a place for you to create application
level objects or variables. With respect to the sample I've initialized three
applications variables - tipDate
, tipText
and
tipID
whenever the site is started. These variables will be used to
hold the current tip information for the current day. Storing this information
means that I don't have to query the database for each user, and so performance
is upped. For more information on the use of the global.asa
file or
Application objects check the links at the end of the article. This is the
relevant code:
sub Application_OnStart 'Tips Defaults Application("tipDate") = "" Application("tipText") = "" Application("tipID") = "" end sub
This html file is the start page of the web application. When the page is
requested the browser will see the OnLoad
event (in the body tag)
and will call the loadtips
function, which will use the JavaScript
Open
to open a new window from the URL it has been passed (our tip
page - tip.asp
). You can also assign titles, and set the properties
(size and location) of the window. For more information regarding the open
function check the links section at the end of the article.
<HTML> <SCRIPT language="JavaScript"> function loadtips() { properties = "toolbar=0,location=0,scrollbars=1,height=300"; properties = properties + ",width=300"; properties = properties + ",left=100,top=100"; popupHandle = open("tip.asp", "tipwindow", properties); } </SCRIPT> <HEAD> <title>Welcome Page</title> </HEAD> <BODY OnLoad="javascript:loadtips()"> . . . </BODY> Tip.asp
This is the ASP the will actually display your HTML page of tips - although it can display anything you like, I chose to simply dump the contents of the Application variables into the tip window. The logic to choose the proper date is shown below.
OpenDB() if(Date <> Application("tipDate")) then if (CheckForExistingTip() = "False") then GetNewTip() end if end if CloseDB()
First I check is to see if the date of the tip in the Application variable is
the same as the system date. So long as it is we are good to go, but if it is
different I have to update the tip stored in the Application variables so that
visitors will use those instead of querying the database. In order to update the
tip we need to determine if a tip for the current date already exists. This is
carried out in the function CheckForExistingTip
.
Function CheckForExistingTip() strSQL = "SELECT ID, TIP_DATE, TIP_TEXT FROM TIPS WHERE TIP_DATE = #" & CDate(Date) & "#" oRS.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly, adCmdText if(oRS.BOF = True and oRS.EOF = True) then 'check for preassigned tip CheckForExistingTip = "False" else oRS.MoveFirst Application.Lock Application("tipID") = oRS.Fields("ID").Value Application("tipText") = oRS.Fields("Tip_Text").Value Application("tipDate") = CDate(oRS.Fields("Tip_Date").Value) Application.Unlock CheckForExistingTip = "True" End If oRS.Close() End Function
CheckForExistingTip
makes a database call specifying the system
date and checks if any rows are returned. When a row is found, then the
Application variables are updated from this data, and the function returns
true
. The Application.Lock
and
Application.Unlock
methods that surround the updating of the
Application variables are important: the Lock
method blocks other
clients from modifying the variables stored in the Application object, and so
ensures that only one client can change application variables at a time. The
UnLock
method releases the lock that was previously placed on the
object. When there is no existing tip found, then false
is returned
and we need to assign a new tip with the function GetNewTip
.
.GetNewTip
queries the tips database and pulls out a tip that
has not been used or assigned to any other date, and are chosen according to the
order of the tip IDs. Application.Lock
and UnLock
are
used again to ensure the update completes successfully.
Function GetNewTip() strSQL = "SELECT ID, TIP_DATE, TIP_TEXT FROM TIPS WHERE TIP_DATE IS NULL ORDER BY ID" oRS.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly, adCmdText if(oRS.BOF = True and oRS.EOF = True) then Application.Lock Application("tipID") = 0 Application("tipText") = "No Tips Available" Application("tipDate") = Date Application.Unlock else oRS.MoveFirst Application.Lock Application("tipID") = oRS.Fields("ID").Value Application("tipText") = oRS.Fields("Tip_Text").Value Application("tipDate") = Date Application.Unlock oRS.Close() UpdateTipDate(Date) End If End Function
A call to UpdateTipDate
finishes the process off.
UpdateTipDate
will update the tips table to assign the system date to the
tip that is now in the Application variable. This guarantees that in the event
of a server crash or reboot, the correct tip will be loaded up again.
So that's how the tips are displayed the tips, and now we need a way to add
new tips, or change existing ones, which is where Admin.asp
comes
in. Three buttons are available at the top of the screen - Refresh, Reset Server
Variables and Add New. Refresh does as you might expect and reloads the page
along with the data table by recalling itself from the webserver. Reset Server Variables will reinitialize the Application
variables to the defaults, and is used mostly for testing. Lastly Add New will send you to an add page
(Form.asp
).
The html table below the three buttons contains the current tips on the
database. Each tip listed has three options available to it – update tip, delete tip or
reset. To update the tip, simply click on the tip
text, which will send you to another page (Form.asp
) that allows
you to carry out the update. Deleting and resetting work pretty much as you
might expect.
The nerve center of the admin page, the function ProcessRequest
,
contains a case statement determining what needs to be done, and is called from
links that are in the html table as well as from the Form.asp
page
described below. These are the functions provided:
Request | Text | Source |
---|---|---|
D | Delete | This request is passed when the Delete
link is clicked. The function requested ("D") along with the tip ID is
contained in the link admin.asp |
R | Reset | This request is passed when the Reset
link is clicked. The function requested ("R") along with the tip ID is
contained in the link. admin.asp |
A | Add | This request comes from the form.asp page and contains
the information needed to add a new tip. |
F | Force | This request is passed when the Reset Server Variables button is depressed. |
U | Update | This request comes from the form.asp page and contains
the information needed to update an existing tip. |
If you want to check how the requests are passed you can either view the html for the page or float over the link and check out the way the link looks in the status bar of your browser.
Within each case statement a SQL statement is dynamically created to reflect the request that has come in. At the bottom of the function the SQL is submitted to the database for processing.
This file is used for both updating an existing tip and adding a new tip. A
flag is passed in the URL to the page to determine which function is being
requested and some minor changes are made based upon the flag. This page will
then post back to the admin.asp
page with a request code to carry
out the work.
To setup the sample simply copy the files onto your server in the same directory and be sure to have a site pointing to the directory. I have used a DSN-less connection so you do not need to setup any DSNs.
In order to make the source clear and easy to follow I did not include error handling at all., and so I'd really recommend adding ADO errors at a minimum. For the same reason I have also not added code to handle special characters in the tip text.
In this article I have shown you how to create a tip of the day or reminder popup screen., and suggested how you might take the idea forward. I've explored the use of some client side JavaScript as well as the ASP Application object.
enter the discussion |
If you would like to contribute to ASPToday, then please get in
touch with us by clicking here.
ASPToday is a
subsidiary website of WROX Press Ltd.
Please visit their website. This article is copyright ©2000 Wrox Press Ltd.
All rights reserved.