By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,407 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,407 IT Pros & Developers. It's quick & easy.

autosum textbox for grand total on form

P: 2
Hi,

I have form with 2 autosummed textboxes ("total" and "casinototal"). I would like to have a grand total textbox ("grandtotal") get its value from summing "total" and "casinototal", but it doesn't seem to be working. Here is my code. Thanks for your help.
-sammie

[HTML]<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Nursing Conference 2007 Registration Form</title>



<script language="javascript" type="text/javascript">
// Calculate the total for items in the form which are selected.
function calculateTotal(inputItem) {
with (inputItem.form) {
// Process each of the different input types in the form.
if (inputItem.type == "radio") { // Process radio buttons.
// Subtract the previously selected radio button value from the total.
calculatedTotal.value = eval(calculatedTotal.value) - eval(previouslySelectedRadioButton.value);
// Save the current radio selection value.
previouslySelectedRadioButton.value = eval(inputItem.value);
// Add the current radio button selection value to the total.
calculatedTotal.value = eval(calculatedTotal.value) - eval(inputItem.value);
} else { // Process check boxes.
if (inputItem.checked == false) { // Item was uncheck. Subtract item value from total.
calculatedTotal.value = eval(calculatedTotal.value) - eval(inputItem.value);
} else { // Item was checked. Add the item value to the total.
calculatedTotal.value = eval(calculatedTotal.value) + eval(inputItem.value);
}
}

// Total value should never be less than 0.
if (calculatedTotal.value < 0) {
InitForm();
}

// Return total value.
return(formatCurrency(calculatedTotal.value));
}
}

// Format a value as currency.
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// This function initialzes all the form elements to default values.
function InitForm() {
// Reset values on form.
document.selectionForm.total.value='$0';
document.selectionForm.calculatedTotal.value=0;
document.selectionForm.previouslySelectedRadioButt on.value=0;

// Set all checkboxes and radio buttons on form to unchecked.
for (i=0; i < document.selectionForm.elements.length; i++) {
if (document.selectionForm.elements[i].type == 'checkbox' | document.selectionForm.elements[i].type == 'radio') {
document.selectionForm.elements[i].checked = false;
}
}
}
</script>




</head>

<body>

<form method="POST" action="--WEBBOT-SELF--" name="conf07reg">
<!--webbot bot="SaveResults" U-File="../_private/form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" startspan --><input TYPE="hidden" NAME="VTI-GROUP" VALUE="0"><!--webbot bot="SaveResults" i-checksum="43374" endspan -->
<table border="0" width="100%" cellspacing="0" cellpadding="0" id="table1">
<tr>
<td>
<p align="center"><b><font face="Verdana" color="#333333">CONFERENCE</font></b></p></td>
</tr>
</table>
<table border="0" width="840" cellspacing="0" cellpadding="0" id="table2">
<tr>
<td width="840" colspan="8"><b>
<span style="font-size: 10.0pt; font-family: Verdana; color: #000066">
FEES AND DEADLINES </span><font size="2" color="#333333" face="Verdana"><br>
&nbsp;</font></b></td>
</tr>
<tr>
<td width="192">&nbsp;</td>
<td width="155" colspan="2">
<p align="center"><b><font face="Verdana" size="2" color="#333333">Early
Registration</font></b></td>
<td width="176" colspan="2">
<p align="center"><b><font face="Verdana" size="2" color="#333333">After
10/2007</font></b></td>
<td width="179" colspan="2">
<p align="center"><b><font face="Verdana" size="2" color="#333333">Staff/Student*</font></b></td>
<td width="138">
<p align="center"><b><font face="Verdana" size="2" color="#333333">Total</font></b></td>
</tr>
<tr>
<td width="192"><b><font face="Verdana" size="2" color="#333333">Main Conference (2
days)</font></b></td>
<td width="78">
<p align="right">




<input type="checkbox" name="early2day" value="298.00" onclick="this.form.total.value=calculateTotal(this );"></td>
<td width="78">
<p align="center"><font face="Verdana" size="2" color="#333333">$298.00</font></td>
<td width="88">
<p align="right">
<input type="checkbox" name="aug2day" value="350.00" onclick="this.form.total.value=calculateTotal(this );"></td>
<td width="88">
<p align="center"><font face="Verdana" size="2" color="#333333">$350.00</font></td>
<td width="90">
<p align="right">
<input type="checkbox" name="student2day" value="175.00" onclick="this.form.total.value=calculateTotal(this );"></td>
<td width="90">
<p align="center"><font face="Verdana" size="2" color="#333333">$175.00</font></td>
<td width="138">
&nbsp;</tr>
<tr>
<td width="192"><b><font face="Verdana" size="2" color="#333333">Main Conference (1
day)</font></b></td>
<td width="78">
<p align="right">
<input type="checkbox" name="early1day" value="170.00" onclick="this.form.total.value=calculateTotal(this );"></td>
<td width="78">
<p align="center"><font face="Verdana" size="2" color="#333333">$170.00</font></td>
<td width="88">
<p align="right">
<input type="checkbox" name="aug1day" value="200.00" onclick="this.form.total.value=calculateTotal(this );"></td>
<td width="88">
<p align="center"><font face="Verdana" size="2" color="#333333">$200.00</font></td>
<td width="90">
<p align="right">
<input type="checkbox" name="student1day" value="100.00" onclick="this.form.total.value=calculateTotal(this );"></td>
<td width="90">
<p align="center"><font face="Verdana" size="2" color="#333333">$100.00</font></td>
<td width="138">&nbsp;</td>
</tr>
<tr>
<td width="702" colspan="7">
<font face="Verdana" size="2" color="#333333"> <input type="radio" name="discount" value="20.00" onclick="this.form.total.value=calculateTotal(this );">
<b>Discount</b> -
$20 per
registrant<br>
&nbsp;</font></td>
<td width="138" valign="top">
<input type="hidden" name="calculatedTotal" value="0">
<input type="hidden" name="previouslySelectedRadioButton" value="0">

<p align="right">

<input type="text" name="total" border="0px" readonly onfocus="this.blur();" size="15" style="text-align: right; font-family: Verdana; font-size: 12px; font-weight: bold"></td>
</tr>
</table>
<table border="0" width="100%" cellspacing="0" cellpadding="0" id="table4">
<tr>
<td valign="top" colspan="4"><b>
<span style="font-size: 10.0pt; font-family: Verdana; color: #000066">
EVENTS</span><font face="Verdana" size="2" color="#333333"><br>
&nbsp;</font></b></td>
</tr>
<tr>
<td valign="top" width="55%">
<font face="Verdana" size="2" color="#333333">Reception </font></td>
<td valign="top" colspan="3">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="55%">
<font face="Verdana" size="2" color="#333333">Party</font></td>
<td valign="top" colspan="3">&nbsp;</td>
</tr>
<tr>
<td valign="top" width="55%">
<font face="Verdana" size="2" color="#333333">Excursion </font></td>
<td valign="top" width="10%">
<font face="Verdana" size="2" color="#333333">
$20/each</font></td>
<td valign="top" width="17%"><font face="Verdana" size="2" color="#333333"># of
tickets
<input type="text" name="casinotix" size="5" style="text-align: center" onblur="this.form.casinototal.value = formatCurrency(this.form.casinotix.value*20)"></font></td>
<td valign="top" width="18%"><font face="Verdana" size="2" color="#333333">$<input type="text" name="casinototal" size="10" border="0px" readonly value="0" style="text-align: right"></font></td>
</tr>
<tr>
<td valign="top" width="55%">&nbsp;</td>
<td align="right" valign="top">
&nbsp;</td>
<td align="right" valign="top">
<font face="Verdana" size="2" color="#333333">TOTAL:</font></td>
<td align="right" valign="top">
<input type="text" name="grandtotal" readonly ></td>
</tr>
</table>
<p><font face="Verdana"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></font></p>
</form>

</body>

</html>[/HTML]
Jul 17 '07 #1
Share this Question
Share on Google+
2 Replies


pbmods
Expert 5K+
P: 5,821
Heya, sammiesue. Welcome to TSDN!

You lost me. Where is the text box?
Jul 18 '07 #2

P: 2
sorry about that -- I mashed code from different sources then tweaked it to get the rest of the form working, so it's not the cleanest code written. Here's the textbox setup:

"Total" - line 163 - this textbox gets its sum from the checkboxes and radio button created in lines 115-161.

"casinototal" - line 192. this textbox gets its sum from the onblur event of the "casinotix" textbox on 191.

"grandtotal" - line 201. this is the textbox that I want to autosum from the amounts in "total" and "casinototal"

I hope that's some help.
Jul 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.