Sams Teach Yourself HTML 4 in 24 Hours

Sams Teach Yourself HTML 4 in 24 Hours

By Dick Oliver

Adding Up an Order Form

One of the most common uses of scripting is making an order form that adds its own totals based on what items the customer selects. Figure 19.6 is an example that you can copy to create self-totaling forms yourself.

19fig06.gif

Figure 19.6 This simple order form uses JavaScript to automatically compute totals.

Although the code in Figure 19.6 is unrealistically simple for any real company's order form (most companies would like at least the address and phone number of the person placing the order), it is a completely functional JavaScript-enhanced Web page. Figure 19.7 demonstrates what the form would look like after a user entered the number 3 in the first box and the number 1 in the third box of the Qty column. The numbers in the Totals column are computed automatically.

19fig07.jpg

Figure 19.7 The JavaScript in Figure 19.6 produces this form. Here, the customer has entered some desired quantities and the form has figured out the total cost.

Most programmers could probably customize and expand the page in Figure 19.6 quite a bit without knowing anything whatsoever about JavaScript. What's more, this page works on any server and any JavaScript-enabled browser on any operating system.

Even if you don't do programming at all, you can easily adapt the code in Figure 19.6 to your own uses. The following list highlights the key elements of this JavaScript that you'll need to understand.

Whew! That may seem like a lot to figure out, especially if you've never done any programming before! With a little experimentation and a few careful readings of this explanation, you should be able to put together a simple automatic order form of your own without any further knowledge of JavaScript.

Share ThisShare This

Informit Network