Page 2 of 22 - Chapter 14
Building an E-Commerce Shop Front
In this chapter we will be building an electronic store front and online ordering
system for an imaginary company called MusicMadOnline.com. It will include many
of the essential elements required for an online store such as the ability to
browse stock, a shopping basket and an online ordering system.
In this chapter you will learn how to:
- Build dynamic pages using server-side JScript v5 and ADO
2.1 to pull information from a SQL Server 7 database
- Validate form based user input using client-side JavaScript
- Persist state from page to page using cookies
- Harness Active Server Page's transaction support via MTS
(Microsoft Transaction Server) to process orders and store user orders in
a database
- Send e-mails using Microsoft's CDONTS (Collaboration Data
Objects for Windows NT Server)
To work through this example, Error!
Cannot open data source.you will need access to Windows NT Server
4 or Windows 2000 with SQL7.0, JScript v5.0, IIS4 or 5, MTS, a simple mail server
and a text editor installed upon it. Note also that where we have split lines
of code onto two or more lines for clarity's sake, this has been marked with
an underscore.
MusicMadOnline.com
Our imaginary client MusicMadOnline.com sells music compact discs and wants
an online store to sell their products. They want customers to be able to browse
their electronic shelves based on music genre. However they don't want their
product lists or the categories to be hardwired in to the HTML pages although
they do require the addition of new products and categories to be reflected
automatically in the HTML pages.
Once the customer has selected their goods they must then be able to place
an order online using a credit card for payment. Once an order has been accepted,
the shipping department must be notified by e-mail of the customer's delivery
address and items to be sent. The customer must also be notified by e-mail that
their order has been accepted and will be delivered shortly.
Design aims
Businesses want to make money and can't afford to turn away customers. It's
for this reason that the client-side part of MusicMadOnline.com has been designed
to work on Internet Explorer and Netscape Navigator browsers version 3 and above.
If we want to include Dynamic HTML effects particular to say IE4 or 5 then separate
pages would most likely have to be written. Although it is possible to include
support for different browsers in the same page, things can quickly get confusing.
On the server-side we have control of what version of JavaScript we use. Here
I have used features available in JavaScript 1.2 and in particular those available
in Microsoft's JScript Version 5.0 which comes with Internet Explorer 5.0 (IE5).
If you don't want to install IE5 on your server than you can download the version
5.0 scripting engine from the Microsoft site (http://msdn.microsoft.com/scripting/jscript/default.htm)
and install it on your server. Perhaps the best reason for upgrading to JScript
v5 is its support of exception handling using the try
catch statement,
but more on this later.
In anticipation of our online store being hugely popular we need to ensure
it can cope with large numbers concurrent users and database transactions. It
may be tempting to use something like Microsoft Access as our back end database,
but as even Microsoft admit, Access won't cut it for high numbers of concurrent
users. Instead I have used SQL Server 7 as the back-end database, though there
are plenty of other high-end databases available which can provide the power
we need.
SQL Server 7 also provides a greater level of sophistication, such as compiled
stored procedures, which by modularizing our database code will make it more
maintainable and as a bonus achieve greater scalability through being pre-compiled.
When dealing with large databases ensuring they don't become corrupted is always
important. By this I mean that we don't want the situation where halfway through
processing a customer's order we hit an unexpected error causing our application
to drop out leaving the database in an inconsistent state. Either a transaction
completes fully and the customer gets their goods or it fails and is rolled
back completely and the customer is informed of this fact and where possible
given information on why it happened and how they can proceed to complete their
order. To help us with this objective we will make use of support for Microsoft
Transaction Server transactions in ASP.
Setting Up The Database
Our first task is to create a new database. Use SQL Server's Enterprise Manager
to create a new database and name it MusicMad. To do this
open up the console root until the name of your server is displayed. Then open
it up to display the current databases.
Right click on Databases in the left
panel and select New Database. Enter the name MusicMad
into the name box and click OK.
The default settings for the database are fine for our purposes.
Next we need to create a new user called MMCustomer with the password Madforit
and give them access to the MusicMad database. In the Enterprise Manager main
console area open up the Security root so you can see Logins and right click
it. Select New Login. Enter MMCustomer for the name, check the SQL Server authentication
radio button and enter Madforit in the password box. Use the drop down combo
box to change the default database to MusicMad.
Then click the Database Access tab at the top of the dialog and check the MusicMad
box to give the user access to our database. Click OK and you'll be asked to
confirm the password. Re-enter it and click OK to close the dialog box.