SHARE
Facebook X Pinterest WhatsApp

Order Form Part 3: “On the Fly” calculations, step one

Written By
thumbnail
Vince Barnes
Vince Barnes
Feb 2, 2005

In the last part of this
short series, we added a form to our
Order Form
web page.  Now it’s time to kick it up a notch! 

Anybody can have a form that asks you what you want, lets you fill it in and
displays what you type.  Bleaagh!  We want something interactive! 
Let’s have our form modify itself as the customer types.  Let’s calculate
all the prices and totals on the fly — displaying the results immediately after
they type, and before they hit "Submit"!

 

No problem!

 

There are several ways
this can be done, but all of them involve running some kind of code on the
client side (on the what? — for more about "sides" see

Goodies To
Go # 207
  in the GTG archives.) 
There are some very powerful and safe technologies for running code on the
client side, such as Microsoft’s .Net (dot net) architecture, but while these
are indeed powerful and sophisticated technologies they also require a fair
amount of programming skill.  There is a technology available to the rest
of us, however, that is relatively simple to use, and is taught in a
Goodies
Tutorial!
  It is, of course,
JavaScript.

 

As a simple client side
scripting language, JavaScript allows us to perform a variety of operations
within the browser on our client’s computer.  It can not, of course,
interact with any files or anything else on the client’s computer (except

cookies),
other than itself and the web page with which it is associated.  That’s
just fine for us….  that’s exactly what we need right now!

 

For our code, we’ll
write a function called "calculate" that will perform all the needed
calculations.  We’ll add the "onchange" event handler to all the quantity
and amount fields in our form to invoke the calculate function each time the
value of any of those fields is altered.  Notice that since some of the
fields are meant to be output only, such as the total amounts, and we still add
the event handler to them, the effect is that if the user modifies one of the
output field, their change is ignored and overwritten.

 

First, here’s the HTML
for our number fields, with the "onchange" event handler call in place:

 

<tr>
<td width="250">Class &quot;A&quot; Widgets</td>
<td align="center" width="100">

<input type="text" name="qtyA" size="5" tabindex="5" onchange="calculate()"></td>
<td align="right" width="60">1.25</td>

<td align="right" width="140">
<input type="text" name="totalA" size="12" tabindex="99" onchange="calculate()"></td>

</tr>
<tr>
<td width="250">Class &quot;B&quot; Widgets</td>
<td align="center" width="100">

<input type="text" name="qtyB" size="5" tabindex="5" onchange="calculate()"></td>
<td align="right" width="60">2.35</td>

<td align="right" width="140">
<input type="text" name="totalB" size="12" tabindex="99" onchange="calculate()"></td>

</tr>
<tr>
<td width="250">Class &quot;C&quot; Widgets</td>
<td align="center" width="100">

<input type="text" name="qtyC" size="5" tabindex="5" onchange="calculate()"></td>
<td align="right" width="60">3.45</td>

<td align="right" width="140">
<input type="text" name="totalC" size="12" tabindex="99" onchange="calculate()"></td>

</tr>
<tr>
<td width="250">&nbsp;</td>
<td align="center" width="100">&nbsp;</td>

<td align="right" width="60">&nbsp;</td>
<td align="right" width="140">&nbsp;</td>
</tr>

<tr>
<td width="250">
<p align="right"><b>TOTALS:</b></td>
<td align="center" width="100">&nbsp;</td>

<td align="right" width="60">&nbsp;</td>
<td align="right" width="140">
<input type="text" name="GrandTotal" size="15" tabindex="99" onchange="calculate()"></td>

</tr>
 

OK!  So now we need
to create our function.

 

Our simple, basic web
page is beginning to gain a little sophistication.  If you’d like to see
the page as it now is, with the form in place,
click here!

 

In the next step, we’ll
add the ability for the form to calculate itself "on the fly".

 

 


Return to the previous step  | 
Proceed to the next step

Recommended for you...

Shopify Alternatives
Helpful Tips for Designing a Landing Page that Converts
Five Essential HTML5 Editors
Best Web Hosting Providers
Enrique Corrales
May 31, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.