-----Original Message-----
I'm sure this is an easy one 'cos I see it all the time!
When in a form, client puts in amount in text? box, now
add 17.5% and show amount below, then add the 2 items
together and place in total box. Should be a piece of
cake?? (FP 2002)
I suppose you want to do this in JavaScript. If so, here's
some sample code:
<html>
<head>
<script>
function calcTotal(){
if (document.forms[0].txtAmt.value == ""){
document.forms[0].txtScg.value = "";
document.forms[0].txtTot.value = "";
return 0;
}
if (isNaN(document.forms[0].txtAmt.value)){
alert("Amount is not a number.");
document.forms[0].txtAmt.focus();
return 0;
}
document.forms[0].txtScg.value =
document.forms[0].txtAmt.value * .175;
document.forms[0].txtTot.value =
parseFloat(document.forms[0].txtScg.value) +
parseFloat(document.forms[0].txtAmt.value);
}
</script>
</head>
<body>
<form method="POST">
<table>
<tr>
<td>Amount</td>
<td><input type="text" name="txtAmt" size="20"
onblur="calcTotal();"></td>
</tr>
<tr>
<td>Surcharge</td>
<td><input type="text" name="txtScg" size="20"></td>
</tr>
<tr>
<td>Total</td>
<td><input type="text" name="txtTot" size="20"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit"
name="btnSub"></td>
</tr>
</table>
</form>
</body>
</html>
However, this suffers from the flaw that visitors can
overtype all three text boxes. One way of solving this is
to add a readonly="true" attribute to the Surcharge and
Title text boxes, but some browsers ignore this. Another
is to display the Surcharge and Total fields as text, but
not all browsers can replace text that's already on
display.
What's more, these sorts of issues are going to get worse
as you continue developing your application. That's why
the better route is usually to perform such calculations
on the server, using ASP, ASP.NET, PHP, or whatever your
Web server supports.
Jim Buyens
Microsoft FrontPage MVP
http://www.interlacken.com
Author of:
*----------------------------------------------------
|\---------------------------------------------------
|| Microsoft Office FrontPage 2003 Inside Out
||---------------------------------------------------
|| Web Database Development Step by Step .NET Edition
|| Microsoft FrontPage Version 2002 Inside Out
|| Faster Smarter Beginning Programming
|| (All from Microsoft Press)
|/---------------------------------------------------
*----------------------------------------------------