PDA

View Full Version : Calculate function not working


MissNightAngel
10-31-2002, 07:26 PM
I am trying to make an order form that will calculate the $ total while the person is filling out the form. The form contains a "qty" box and several check boxes that need to be figured into the total. The function script I have is not working when I add the check boxes but it will work if I use the input=text boxes like the qty box. I have tried using onClick instead of onBlur for the checkboxes and it didn't help. How can I change the function script to include the check boxes in the total?

<script>
function calculate(what) {
for (var i=1,answer=0;i<5;i++)
answer += what.elements['txtSold' + i].value - 0;
what.txtSoldTotal.value = answer;
}


function calculateGrand(grand) {
for (var i=1,answer=0;i<5;i++)
answer += grand.elements['txtGrand' + i].value - 0;
grand.txtGrandTotal.value = answer;
}

function itemTotal(item){
doc = "document.frmOrder";
eval ("document.frmOrder.txtGrand"+item+".value = document.frmOrder.txtCost" + item +".value * document.frmOrder.txtSold"+ item + ".value");

}
</script>
<form>
<input type="hidden" name="txtCost1" value="268.95">
<input type="hidden" name="txtCost2" value="99.95">
<input type="hidden" name="txtCost3" value="99.95">
<input type="hidden" name="txtCost4" value="99.95">
<input type="hidden" name="txtCost5" value="99.95">
<input type="hidden" name="txtCost6" value="49.95">


<table border=1 cellpadding=1 cellspacing=0 width="65%" align=center valign=middle>
<TR BGCOLOR="#666666"><td width="100%" align=center valign=middle>&nbsp;ITEMS:</td></tr>
<TR><TD align=center valign=middle>
Style 1 -- 35 Cigars &nbsp; &nbsp;
Qty<input type=text size=4 name=cigarbox1qty value="" onBlur="calculate(this.form); itemTotal('1'); calculateGrand(this.form);">
<BR>
<input type=checkbox name=style1color value="black">Black
<input type=checkbox name=style1color value="brown" onBlur="calculate(this.form); itemTotal('2'); calculateGrand(this.form);">Brown
<input type=checkbox name=style1color value="blue" onBlur="calculate(this.form); itemTotal('3'); calculateGrand(this.form);">Blue
<input type=checkbox name=style1color value="red" onBlur="calculate(this.form); itemTotal('4'); calculateGrand(this.form);">Red
<input type=checkbox name=style1color value="green" onBlur="calculate(this.form); itemTotal('5'); calculateGrand(this.form);">Green<BR>
<input type=checkbox name=style1color value="clear">Clear with Red Cedar
<input type=checkbox name=style1color value="addcedar" onBlur="calculate(this.form); itemTotal('6'); calculateGrand(this.form);">Add Red Cedar Lining
&nbsp; &nbsp;
<TR><TD align=center valign=middle><input type="hidden" name="txtSoldTotal" size="6" value="" >
Subtotal:$<input type="text" size="9" name="txtGrandTotal" value="">
</TABLE><center>
<input type=submit name=submit value="Send Order" STYLE="background:FFCC99">
<input type=reset name=reset value="Clear Form" STYLE="background:FFCC99">
</FORM>



:( :confused: :(

ConfusedOfLife
10-31-2002, 08:16 PM
I don't like this spaghetti code ( sorry! ), why don't you use an array instead of those hidden values? And whenever a checkbox is checked, it's no need that you calculate the grand total for all your elements, it's just enough to add that item's cost to the variable that holds your grand total, and whenever a checkbox is unchecked, you subtract it.
You can define the array like this:


myCosts = [ 32.5,
45.5,
52.5,
.
.
99.95
]
// This is gonna hold your grand value.
myGrand = 0;

Then for example for your first element, assuming that it's a checkbox you can have :


<input type='checkbox' onclick = ' if ( this.checked ) add2Grand(1); else remFromGrand(1);'>

You can write your add2Grand like this :


function add2Grand(which, howMany)
{
// if howMany wasn't supplied, for example in case of
// a checkbox, we assign one to it.
if ( !howMany ) howMany = 1;
myGrandValue += howMany * myCosts[which];
}


You know how to write the remFromGrand now! For any check box that holds the number of orders, for example the number of ciggarets you write ( assuming that your ciggarete text box is your fourth element in the myCosts array ):

<input type = 'text' onblur = ' add2Grand(4, parseInt(this.value))'>


I hope that it helps!

MissNightAngel
10-31-2002, 09:56 PM
Would I need to change the add2Grand numbers like:
<input type='checkbox' onclick = ' if ( this.checked ) add2Grand(1); else remFromGrand(1);'>
<input type='checkbox' onclick = ' if ( this.checked ) add2Grand(2); else remFromGrand(2);'>
<input type='checkbox' onclick = ' if ( this.checked ) add2Grand(3); else remFromGrand(3);'>

What would be the code I use to get the subtotal to show up?

ConfusedOfLife
11-01-2002, 10:01 PM
Yes, of course! I'm just trying to put these lines of your original code into an array :


<input type="hidden" name="txtCost1" value="268.95">
<input type="hidden" name="txtCost2" value="99.95">
<input type="hidden" name="txtCost3" value="99.95">
<input type="hidden" name="txtCost4" value="99.95">
<input type="hidden" name="txtCost5" value="99.95">
<input type="hidden" name="txtCost6" value="49.95">


So, you put all the values into an array, that's myCosts array, and if for example the first cost belongs to a checkbox, you write :

<input type='checkbox' onclick = ' if ( this.checked ) add2Grand(0 ); else remFromGrand(1);'>

I put 0 in there because your array's index starts with zero, means myCosts[0] = 268.95, myCosts[1] = 99.95 and so on.

If you noticed, I defined a global variable ( myGrand ) to hold your costs, you should define it at the begining of your script, but when for example a check box is checked and you send the index of your relevant cost to add2Grand function, it's gonna add the relevant cost to myGrand variable that then you can access whenever you want.
I also put that howMany argument for add2Grand function to address a text input, because contrary to a checkbox that when it's checked we're sure that the customer just wants ONE item of that special good ( we decided to design it in this way ), a text box gives the customer the choice of entering how many numbers he/she wants, so, whenever we have a text box, we should send it's value ( this.value ) to our function ( add2Grand ) as the second variable to calculate the grand total correctly, clearly we have to set the proper index to find the relevant cost ( through the first argument ) to the array. For example if someone wants 3 books that each book costs $5, then we should add 15 dollars to myGrand.
I also put a default value for howMany, i.e. 1, that you don't have to enter 1 each time that you have a checkbox.
I didn't write the remFromGrand and I'm sure that you can write it yourself ( change the name too! that FROM in the name isn't that beautiful!! )

MissNightAngel
11-02-2002, 12:41 AM
Thank you for the help. I will play around with the code and see if I can get it to work for me.

ConfusedOfLife
11-03-2002, 08:10 PM
You're welcome, feel free to ask any question that you have and I do my best to answer...