Learn how to process form data
Takeaway: After a user has entered valid data, you can use JavaScript to process the form data. Here's how.
By Emily A. Vander Veer
Once you've verified that the user has entered valid data, it's time to do something with it. In JavaScript, that's fairly easy.
We'll calculate the total adoption fee with the following function:
Simple, eh? The calcTotal() function simply takes the number supplied by the user in the numberOrdered field and multiplies it by 15.99.
The calculated total then gets printed in the "Total sponsorship fee" box.
And, of course, here's the HTML used to call the function:
This sends the number entered in numberOrdered to calcTotal() for processing, assuming it passes the isANumber() test. Notice that this JavaScript example doesn't require that the data be sent to the server for final processing. You can use similar techniques to create all kinds of online calculators, converters, or quizzes.
Note that at the beginning of our script, we define orderPlaced (a global variable) and initialize its value to "false."
If the user enters a number in the "number of brains" field, we change the value of orderPlaced to "true" by adding these lines to the isANumber() function:
We use this variable as follows:
Here, if the user attempts to change the value of the totalPrice field after a valid order has been placed (orderPlaced has been set to "true"), an alert pops up informing the user she is not allowed to change that field. In addition, calcTotal() is called again to restore the value of the totalPrice field.
function calcTotal()
document.orderForm.totalPrice.value =
(document.orderForm.numberOrdered.value
* 15.99);
}
Number of brains you'd like to sponsor
<INPUT TYPE="text" NAME="numberOrdered"
VALUE="" SIZE=10
onChange="if(this.value) {
if (!(isANumber(this.value, 'number of
brains'))) {
this.focus();
} else {
calcTotal();
}
}">
var orderPlaced = false;
if (answer == 1) {
orderPlaced = true;
}
Total price
<INPUT TYPE="text" NAME="totalPrice" SIZE=10
onChange="if (orderPlaced) {
alert('Total fee is a calculated field.');
calcTotal(this.value);
}">
Emily A. Vander Veer is the author of numerous Internet-related magazine articles and books, including JavaScript for Dummies, JavaScript for Dummies Quick Reference, and JavaBeans for Dummies, all published by IDG Books.
SponsoredWhite Papers, Webcasts, and Downloads
- IBM Master Data Management: Effective Data Governance IBM
- Sprint IPVoice Connect Fact Sheet Sprint
- Case Study: GHS Data Management - Improving Data Protection and Storage Reliability for Critical Databases Dell EqualLogic
- Yankee Group: Exploring the Benefits of 3G Wireless Integrated into Business-Class Routers Sprint
- Case Study: Clackamas County Oregon's Outdated Fibre Channel Infrastructure Runs Out of Capacity Dell EqualLogic
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET


