HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This compact, example-rich guide teaches Web developers exactly what they need to know to build great dynamic sites and applications with Microsoft's ASP and JavaScript, the world's #1 Web scripting language. Endorsed by the World Organization of Webmasters, Essential ASP for Web Professionals offers no-nonsense, practical coverage built around real-world examples on a live sample Web site. Every chapter includes real-world overviews of key ASP and JavaScript features, reinforcing what you've learned and demonstrating how to adapt it quickly to your own applications. From the basics of embedding ASP code in HTML pages to full-blown database application development, this book delivers practical answers, usable code, and real solutionsfast!
Use ASP and JavaScript to do all this, and more!
Look to Essential Books for ALL the Web Skills You Need!
All these books share the same great format and similar Web sites containing downloadable code-so once you've used one, learning from the others is a piece of cake!
Download
code for Project 1 (Zipped ASP file)
Download
code for Project 2 (Zipped ASP file)
Who knew adding the date to a web page could be so fun? Time just flies when you're learning the basics of creating ASP pages with a couple of suitably trivial examples.
Project 1:
A Simple Date Script
In which ASP uses funny looking "<% %>" tags to insert code into a perfectly good web page.
Project 2:
A More Powerful Date Script
Things heat up with a script that is more than one line long, an introduction to JavaScript data types, and other basics.
Chapter 2 Reading and WritingDownload
code for Project 1 (Zip archive)
Download
code for Project 2 (Zip archive)
A couple of projects that involve reading from and writing to text files stored on the web server's hard drive.
Project 1:
Add Data from a Text File to a Web Page
A "tip of the day" script. Information is drawn from a text file into a text page. This is about the easiest content management system you can build or use, for that matter. (See Chapter 4 for a more complicated content management system.)
Project 2:
Writing Form Data to a Text File
Maybe I should have called this project "Guest Book Script." Well, with the book at the printer, too late for that. That's what Project 2 is, though, with information being submitted by visitors to your site and stored in a text file on the server.
Chapter 3 Database-Driven PagesDownload
code for the project (Zip archive)
Using a database that stores very simple directory information about people, events, and web sites, the chapter presents a set of scripts that allow you to publish and navigate the contents of the database.
Project
Generating Web Pages From a Database
ASP provides a very nice way to get information out of a database and onto a web page. Open database connections! Create recordsets! What are you waiting for go buy this book!
Chapter 4 Editing Records Download code for the project (Zip archive)Using the same database as Chapter 3, this time the scripts get a little more complicated and create a password protected interface that can be used to edit the contents of the database.
Project
Using a Web Interface to Edit Records in a Database
Rather dry project name notwithstanding, this chapter has some nice stuff in it, such as code that automatically iterates through incoming form information and stuffs it into a database without knowing what the field names are beforehand. If you haven't had to deal with this before, it's a nice trick when you add or remove fields to or from the database later. A long chapter with a lot of code.
Chapter 5 Shopping Cart Download code for the project (Zip archive)If you're building Web sites, someone will eventually want you to build a shopping cart for their site. Presented here for your reading and coding pleasure, one way to solve that problem.
Project
Building a Shopping Cart
A simple yet comprehensive set of cart scripts with the usual feature set: display the stuff that's for sale, including support for multiple options; search for stuff by category and keyword; add stuff to the cart, edit quantity, or remove items; and collect shipping and billing information.
Chapter 6 Email Download code for the project (Zip archive)Sending email from a web page is easy. This is a short chapter. What more could you ask for?
Project
Simple Email Script
Using the CDONTS object, learn to send email in as few as two lines of code.
Scripts that are actually useful with less than 20 lines of code.
Click here for a sample chapter for this book: 0130304999.pdf
1. Keeping Time.
Project I: A Simple Date Script. New Features. Script 1-1 date_simple.asp. Project II: A More Powerful Date Script. New Features. Script 1-2 date_fancy.asp.
Project I: Add Data from a Text File to a Web Page. New Features. Code for Tip of the Day. Script 2-1-1 tips.txt. Script 2-1-2 tip_of_the_day.asp. Project II: Writing Form Data to a Text File. New Features. Script 2-2 guest_book.asp.
Project: Generating Web Pages from a Database. New Features. Code for View Database Scripts. Script 3-1 ch3_include.js. Script 3-2 ch3_view_categories.asp. Script 3-3 ch3_view_records.asp.
Project: Using a Web Interface to Edit Records in a Database. New Features. Code to Edit Database. Script 4-1 ch4_password_form.asp. How the Script Works. Script 4-2 ch4_include.js. Script 4-3 ch4_list_categories.asp. Script 4-4 ch4_edit_category_form.asp. Script 4-5 ch4_update_category.asp. Script 4-6 ch4_delete_category.asp. Script 4-7 ch4_blank_category_form.asp. Script 4-8 ch4_insert_category.asp. Script 4-9 ch4_list_records.asp. Script 4-10 ch4_edit_record_form.asp. Script 4-11 ch4_update_record.asp. Script 4-12 ch4_delete_record.asp. Script 4-13 ch4_blank_record_form.asp. Script 4-14 ch4_insert_record.asp. Script 4-15 ch4_edit_subcategory_form.asp. Script 4-16 ch4_insert_subcategory.asp. Script 4-17 ch4_delete_subcategory.asp.
Project: Building a Shopping Cart. Security. A Quick Tour. New Features. The ASP Shopping Cart Code. Script 5-1 cart_list_items_for_sale.asp. Script 5-2 cart_set_session.asp. Script 5-3 cart_view_contents.asp. Script 5-4 cart_change_qty.asp. Script 5-5 cart_remove_all.asp. Script 5-6 collect_shipping_information.asp. Script 5-7 set_shipping_information.asp. Script 5-8 collect_billing_information.asp. Script 5-9 set_cc_billing_information.asp. Script 5-10 set_pwc_billing_information.asp. Script 5-11 order_complete.asp. Script 5-12 cart.js. Script 5-13 view_session_contents.asp.
Project: Simple Email Script. New Features. Code. Script 6-1 form.asp. Script 6-2 catch_form.asp.
Welcome to the book.
This is an ASP cookbook. Each chapter contains one or more recipes that work together in a useful way. Briefly:
All of these recipes rely on Active Server Pages (ASP) and JavaScript, which is mostly what this introduction is about.
Tim Berners-Lee started things off with a great way to publish documents called HTML, which is a wonderful way to publish static documents.
Pretty quickly thereafter, something called CGI was added to Web servers to let programmers generate HTML pages on the fly. With CGI, when the Web server gets a request for page, it starts a program and hands the request to the program. If the program generates any output, that output is returned to the browser.
CGI is a pretty great way to do a lot of things, but it's a very program-ish solution, which is to say it's not very HTML-ish. When you look at a CGI program, most of the time, it doesn't look much like an HTML page, even though that's usually the end result. And if there's a lot of HTML in the program, a lot of the time it has to be carefully wrapped in quotes to separate the program parts from the HTML parts, which can get tiring.
Then there's the performance issues: Most CGI programs are written using scripting languages like Perl, which is ideally suited for this kind of thing.
The problem with this is that every time a Web browser asks for a Web page that's generated by a CGI program, a new process is launched on the server. Launching new processes is kind of slow and takes up a lot of resources. Programs with too many simultaneous CGI programs can get very bogged down.
There are, of course, ways around this, but they involve doing fancy technical things that are a lot harder than writing Perl scripts.
So it became clear that there needed to be an easier way to generate dynamic Web pages.
The solution that has emerged to the HTML vs. CGI debate is to do both. Increasingly, Web servers are getting smarter, so that it's possible to drop little program snippets inside of Web pages instead of having to maintain programs and HTML separately.
This isn't the end-all, be-all for the Web. Small sites that don't need dynamic content should still be created with HTML.
Very large sites with millions of pages and complicated applications need to be built using complicated publishing systems and application servers.
But for a lot of things, being able to mix HTML and programming is an excellent solution. And that's what ASP is all about.
ASP is probably the best way to mix HTML and simple programming in a Windows environment. ASP pages can also be hosted on a UNIX server, using software from ChiliSoft. But most people use ASP with Windows NT and Windows 2000, because it's built into IIS, it's easy to use, and it's powerful.
ASP is not the only game in town. Other products that let you mix HTML and programming include PHP, Cold Fusion, and JSP. But if you're interested in building dynamic product on the Windows platform, you'll find that ASP is often the first choice for developing dynamic Web sites.
If you're comfortable with HTML source code, adding ASP to your arsenal will feel like the logical next step.
This book assumes that you are comfortable with HMTL source code. If you are not familiar with HTML source code, you will want to get a good HTML book to refer to while you work with this book.
Let's start with a real simple HTML page called hello_world.html
:
<html><head><title>Hello World</title></head><body>Hello World</body></html>
If your Web server is IIS (you need IIS or a Web server that supports Active Server Pages for this to work), you can take this file and rename it hello_world.asp
:
<%@ Language=JavaScript %><html><head><title>Hello World</title></head><body><% Response.Write("Hello World") %></body></html>
Three things have changed:
.html
, the file now ends with .asp
. This tells the Web server that there may be some code on the page. Instead of just giving the page to a Web browser when someone types in the URL of the page, the Web server checks the page for <% ... %>
tags first. If it finds any, it removes the tags and their contents, and replaces them with the output of any program that is inside of them.<%@ Language=JavaScript %>
. This tells the Web server that the programming language is JavaScript. Because IIS, the Microsoft Web server, often assumes that ASP programs will be written in VBScript, it's always a good idea to tell IIS if you're using JavaScript.<% ... %>
tags) is the line <% Response.Write("Hello World") %>
. The Response.Write()
command is a lot like the print()
command in many programming and scripting languages. In the context of ASP, this code tells the server to send the string "Hello World
" to the browser.The net result is that the .asp
page and the .html
shown above have exactly the same result: a Web page with the words "Hello World
" on it.
Nevertheless, the two pages are very different. When a browser asks the Web server for an .html
page, the server passes the page to the browser without much thinking about it. On the other hand, the Web server looks at .asp pages to check for small programs to run. This means that any or all of the contents of an .asp
page can be generated on the fly. This book will explore how this can be useful.
Most books about ASP use the VBScript scripting language. Rather than use VBScript, this book uses JavaScript, for a number of reasons:
JavaScript first emerged on the scene as a scripting language for the Netscape browser. Later, when you could use JavaScript with Microsoft Internet Explorer, I think they called it JScript (at any rate, Microsoft often calls it JScript now).
Because Netscape and Microsoft couldn't agree on how JavaScript/Jscript should work, it was hard to do anything besides rollover buttons that would work reliably.
For the last couple of years, there's been something called ECMA Script that's supposed to be an open protocol that will bring everything back together or something.
Because all the code in this book was written for Microsoft Active Server Pages, it seems likely that one could argue that the code is JScript. Yet, this book talks mostly about JavaScript, rather than JScript. I've been able to find two reasons why: First, I think JavaScript sounds better than JScript; Second, the code that I use in my scripts to declare the language is <%@ Language=JavaScript %>
. And this works.
Whatever.
The reason that ASP, IIS (the Microsoft Web server), and JavaScript are worth using is that they make it easy to create a script quickly, using a suite of tools that let us limit our focus to the specific task confronting us without having to reinvent the wheel.
When our script needs to perform a task, the bulk of the task can often be performed by a built-in ASP object, Microsoft scripting object, or JavaScript object. Some of the important ones are listed below.
One of the tasks that used to be a real hassle in the early CGI days was collecting information from a form. For example, if a form collects a variable called email
that contains an e-mail address, you can easily access it with the Request object:
email_address = Request("email");
Another fun trick is the ability to redirect a visitor to a different page, using the ASP Response object:
Response.Redirect("http://www.lovejoy.com/redirect_page.asp");
which is a lot faster way to redirect someone than using a <meta> tag.
And, of course, there are a lot of other neat tricks that make it fun and worthwhile to work with ASP and JavaScript! Thus, this book...
As the name implies, ASP objects are collections of logically related methods and properties. The table below lists the most commonly used ASP objects and the purposes they serve.
ASP Object | Purpose |
---|---|
Request | Manages information about the request that the browser sends to the Web server. Often, the most important element of this request is incoming form information. |
Response | Manages information that is being sent to the browser in response to the browser's request to the Web server. For example, the Response.Write() command can be used to send HTML to the browser. |
Server | Manages information that is related to the server. For example, the Server object can be used to find the URL of the current script. |
Session | Manages information about a user's total visit. For example, if a user looks at five pages when visiting a site, the Session object can be used to keep track of that user as she looks at the five pages. |
In addition to objects that are unique to ASP, it is possible to use Microsoft scripting objects from within the ASP environment. For example:
Microsoft Scripting Object | Purpose |
---|---|
FileSystem | Can be used to read and write information to and from the filesystem, including looking at the contents of existing files and creating new files. |
ADODB | A database technology that can be used to read and write information stored in databases such as MicrosoftAccess, Microsoft SQL Server, or Oracle. |
CDONTS | Contains an object called NewMail that can be used tosend email from a script. |
Finally, the JavaScript language includes some built-in objects that help with certain tasks. For example:
JavaScript Object | Purpose |
---|---|
Date | Does the legwork necessary to work with dateswho has time to keep track of days, months, years, etc...? |
Math | A variety of useful math tricks such as generatingrandom numbers and rounding off real numbers. |
This book consists of a series of fully working programs that can usually be set up in a matter of minutes. Unlike CGI scripts, where you often have to worry about permissions and other configuration issues, the ASP scripts in this book can be FTPd to just about any Windows NT or Windows 2000 server (or even a Win95/98 PC with personal Web server installed) and be up and running immediately.
If you're new to Web scripting/programming, it's probably a good idea to use Chapter 1 as a tutorial.
After that, this book can be used as a cookbook: Simply jump directly to the script that looks interesting or otherwise meets your needs, and you should be up and running quickly, whether you're a novice or an experienced programmer working with ASP for the first time.
The appendixes contain reference information on topics that relate in one way or another to getting ASP scripts up and running quickly and easily.
All code examples are identified by the use of the Courier
fixed width font. If there are line numbers next to an example of code:
1.// Some code examples2.// have line numbers next to them.
1,2.The code will usually be followed by detailed explanations of how the code works.
Note that, in some cases, a line of code will wrap to the next line. That does not mean that there is a line break in the code! You can generally tell a wrapped line because there is no line number next to the part of the line that has wrapped. Putting a line break where a line wraps can cause some odd behavior that's surprisingly difficult to track down, so be careful with this one. Don't want to worry about line breaks? Don't! Go to the companion Web site and download the code: http://www.phptr.com/essential/
.
Each chapter is structured as follows:
This book assumes that you've got at least a passing familiarity with HTML. If you haven't worked directly with HTML before, you'll probably want an HTML resource to turn to.
JavaScript is case-sensitive. sO be cAreful about how you type things. If something doesn't work right, you can explore changing the case...
Line 13 of ch4_update_record.asp should be:
if (temp_field.search(/co_|toss|current_/) < 0 ){
ch4_delete_record.asp in chapter 4 is an old version of the script and is incompatible
with the other code in chapter 4."