Click to See Complete Forum and Search --> : Please help with a calculation
DarryBoy
04-18-2003, 03:35 PM
Hi Guys
Please click on the following link to follow what i'm trying to achieve http://www.sunscreen4africa.co.za/sunscreen_orderform1.htm
When a user enters a quantity (QTY) and then clicks on either single or case check box that have values 130 and 1300 respectively. The total qty must reflect a value of the check boxes mentioned X (*) the qty entered instantly.
I will replicate this formula throughout the rows. I then need to add all these rows together to give me the Consumer Price Sub-Total and then need to add the Postal cost (30) to give me the total due.
Your help is most appreciated. Thank you in advance.
Kind regards
Darryn
Hmmmm.... I'm not sure what you're asking for, but this might help a little.
<html><head>
<script>
function calc(){
document.formName.totalQTYName.value=parseInt(document.formName.qtyName.value)*document.formName.che ckBoxName.value;
}
</script>
</head><body>
<form name="formName">
<input type="text" name="qtName" onkeydown="calc()"><br>
<input type=checkbox name="qtyChk" value="130"> Single<br>
<input type=checkbox name="qtyChk2" value="1300"><br>
<input type=text name="totalQTYName">
</form></body></html>
DarryBoy
04-20-2003, 12:20 PM
Thank you Jona. I recieved a "NaN" in the totalQTYName. What does it mean?
I don't know which checkbox the user will choose, 130 or 1300. How would I make the destinction in code? The user must not be able to put a check in both boxes.
I look forward to your reply.
Kind regards
Darryn
DrDaMour
04-20-2003, 12:31 PM
then use radiobuttons, not checkboxed, and nan is not a number. That happens when a box isn't filled out but is being used for calculation.
DarryBoy
04-20-2003, 12:52 PM
A checkbox is more friendly for a user because of the checkmark. Only one checkbox maybe selected with the right code.
My form is a calculation so your definition of nan isn't that clear.
havik
04-20-2003, 12:57 PM
nan stands for "not a number". When the calculation is made and nan shows up, it means that the result is not a number. So there are errors that need to be fixed.
I agree with using radio buttons since that's what they were created for. But if you want to use checkboxes then have a look at this code and alter it:
http://javascript.internet.com/forms/limit-boxes.html
Havik
DrDaMour
04-20-2003, 01:29 PM
nan is not a number. That happens when a box isn't filled out but is being used for calculation.
how is that not clear? you're using a text box in yoru calculation that doesn't have a value
Okay, I must still be confused with what the original question was. Are you multiplying by 130 or 1300 depending on which checkbox was checked (and they can only check one)? And then when they put something in the box on the left (QTY) it calculates it and puts it in the box to the right (totalQTY)?
DarryBoy
04-20-2003, 06:27 PM
Thank you Havik for your clear explanation and the link to the javascript code.
DarryBoy
04-20-2003, 06:30 PM
Yes to your questions Jona. I have a few errors with the code you gave me. Did the code work for you?
I never tested the code. It was meant as an example. Anyways, one more question before I go off and work on this.. Do you want it where, when they click the first checkbox and then the second one, the first one becomes unchecked? Just like a radio button? Or do you want it where, then they click the first checkbox then the second it alerts saying "you can't check both!" and unchecks the second?
DarryBoy
04-20-2003, 07:11 PM
Do you want it where, when they click the first checkbox and then the second one, the first one becomes unchecked? Please
Is it possible also to reflect the total once a user checks one of the boxes ie a user enters a qty and then checks a box. These values are multiplied (*) and the value is displayed in the total.
Will this do?
<html><head>
<script>
function calc(){
var chkd;
var count = 0;
if(document.qtyFrm.c1.checked){chkd=document.qtyFrm.c1.value; count=count+1;}
if(document.qtyFrm.c2.checked){chkd=document.qtyFrm.c2.value; count=count+1;}
if(count==2){alert("Check only one, please."); return false;}
document.qtyFrm.qtyTotal.value=parseInt(document.qtyFrm.qty.value)*chkd;
}
</script>
</head><body>
<form name="qtyFrm">
<input type=text name="qty" onkeypress="calc()"><br>
130: <input type=checkbox name="c1" value="130" onclick="return calc()"><br>
1300: <input type=checkbox name="c2" value="1300" onclick="return calc()"><br>
<input type=text name="qtyTotal">
</form></body></html>
DarryBoy
04-20-2003, 07:52 PM
Thank you Jona, I really appreciate your help.
How do I remove the error msg? Is it OK for me to remove the msg from the code without a problem in the count function? A user must be able to click on one and when the other is clicked the other deselects without an error msg display.
When I enter a qty value a nan error is displayed in the total. How do I leave the total blank and only display a value when a user clicks one of the checkboxes?
Okay, just take out this line to remove the alert: alert("Check only one, please.");
To take out onkeypress="calc()" on the first input.
DarryBoy
04-21-2003, 05:14 PM
Thank you Jona you have put me on track.