Click to See Complete Forum and Search --> : How should I use this calculation?


DarryBoy
04-22-2003, 07:52 PM
Jona in the following (all credit to you). How do I use the calc for the text fields in the second and third. If I use the same calculation for them I will have many lines of code. Is there a way to alter the function to calculate the following.

<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;
if(document.qtyFrm.qtyTotal.value.indexOf("$")){ document.qtyFrm.qtyTotal.value="$"+document.qtyFrm.qtyTotal.value+".00";}
}
</script>
</head><body>
<form name="qtyFrm">
<p>
<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">
</p>
<p>
<input type="text" name="textfield">
<br>
130:
<input type="checkbox" name="checkbox" value="checkbox">
<br>
1300:
<input type="checkbox" name="checkbox2" value="checkbox">
<br>
<input type="text" name="textfield2">
</p>
<p>
<input type="text" name="textfield3">
<br>
130:
<input type="checkbox" name="checkbox3" value="checkbox">
<br>
1300:
<input type="checkbox" name="checkbox4" value="checkbox">
<br>
<input type="text" name="textfield4">
<br>
</p>
</form></body></html>

DarryBoy
04-22-2003, 08:07 PM
If you copy this code to a web editor and then run it I think you will better understand what I'm asking.

DarryBoy
04-22-2003, 08:43 PM
The code below has errors. Can you see what I'm trying to do? Someone please help.

<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(document.qtyFrm.c3.checked){chkd=document.qtyFrm.c3.value; count=count+1;}
if(document.qtyFrm.c4.checked){chkd=document.qtyFrm.c4.value; count=count+1;}
if(count==2){alert("Check only one, please."); return false;}
document.qtyFrm.qtyTotal.value=parseInt(document.qtyFrm.qty.value)*chkd;
if(document.qtyFrm.qtyTotal.value.indexOf("$")){ document.qtyFrm.qtyTotal.value="$"+document.qtyFrm.qtyTotal.value+".00";}
}
</script>
</head><body>
<form name="qtyFrm">
<p>
<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">
</p>
<p>
<input name="qty" type="text" id="qty">
<br>
130:
<input name="c3" type="checkbox" id="c3" value="130" onclick="return calc()">
<br>
1300:
<input name="c4" type="checkbox" id="c4" value="1300" onclick="return calc()">
<br>
<input name="qtyTotal" type="text" id="qtyTotal">
</p>
<p>
<input type="text" name="textfield3">
<br>
130:
<input type="checkbox" name="checkbox3" value="checkbox">
<br>
1300:
<input type="checkbox" name="checkbox4" value="checkbox">
<br>
<input type="text" name="textfield4">
<br>
</p>
</form></body></html>

DrDaMour
04-22-2003, 09:21 PM
Originally posted by DarryBoy

chkd=document.qtyFrm.c1.value; count=count+1;}
if(document.qtyFrm.c2.checked){chkd=document.qtyFrm.c2.value; count=count+1;}
if(document.qtyFrm.c3.checked){chkd=document.qtyFrm.c3.value; count=count+1;}
if(document.qtyFrm.c4.checked){chkd=document.qtyFrm.c4.value; count=count+1;}


every time you are reassigning the chkd value, instead you want to add the value to chkd.

if i have
var i = 0;
i add 20 by
i = i + 20;
or
i += 20;

DarryBoy
04-23-2003, 03:40 PM
Thank you DrDaMour for your reply. I'm very new to programming and please ask if you may give me a little more help.

Kind regards
Darryn

DarryBoy
04-23-2003, 04:24 PM
Please may anyone help me with the above or lead me to a tutorial or an example.

Kind regards
Darryn

Jona
04-23-2003, 05:36 PM
Oh, multiple, eh? Here, try this dynamic function....

function calc(qty, total, price){
var frm = document.forms[0];
var xpr, zlp, sub_2;
if(total.value.indexOf(".") != -1){
xpr = total.value.indexOf(".");
zlp = total.value.length;
sub_2 = total.value.substring(xpr, zlp);
}
if(isNaN(total.value)){total.value="";}
total.value="$"+qty*price;
}

Your form (example):

<form action="">
<td><input name="qty_mealreplaceshakevan" size="5" onkeydown="calc(this.value,this.form.total_mealreplaceshakevan,'29.95');" onkeyup="calc(this.value,this.form.total_mealreplaceshakevan,'29.95');"></td>
<td>Meal Replacement Shake(vanilla)</td>
<td>$29.95</td>
<td><input name="total_mealreplaceshakevan" size="15"></td>
</form>

DarryBoy
04-23-2003, 06:21 PM
Thank you Jona for this. I used this example and created another 2 fields and description in between as follows but I get an error on line 5.

<script>

function calc(qty, total, price){
var frm = document.forms[0];
var xpr, zlp, sub_2;
if(total.value.indexOf(".") != -1){
xpr = total.value.indexOf(".");
zlp = total.value.length;
sub_2 = total.value.substring(xpr, zlp);
}
if(isNaN(total.value)){total.value="";}
total.value="$"+qty*price;
}
</script>
Your form (example):

<form action="">
<td><input name="qty_mealreplaceshakevan" size="5" onkeydown="calc(this.value,this.form.total_mealreplaceshakevan,'29.95');" onkeyup="calc(this.value,this.form.total_mealreplaceshakevan,'29.95');"></td>
<td>Meal Replacement Shake(vanilla)</td>
<td>$29.95</td>
<td><input name="total_mealreplaceshakevan" size="15"><br>
<input name="qty_mealreplaceshakevan" type="text" size="5" onkeydown="calc(this.value,this.form.total_mealreplaceshakevan,'39.95');" onkeyup="calc(this.value,this.form.total_mealreplaceshakevan,'39.95');"> </td>
<td>Meal Replacement Shake(chocolate)</td>
<td>$39.95</td>

<input name="total_mealreplaceshakevan" type="text" size="15">
<br>

</form>

It's probably very silly but I'm trying my best.

Jona
04-23-2003, 06:34 PM
That was meant as an example.. :) Not for what you want. Here, how about I give you a quick tutorial on this....

<script>
function calc(qty, total, price){ /*This function is sent variables, and will not function without them. If I did onClick="calc()" I would get an error. You must send values in the variables. So if I had var x = 1 it would be the same as: function calc(x), and an event handler to start the function: onclick="calc(1)" when I run the function calc(x), and try to alert(x) it would return "1." See what I mean? In this way, we are sending 3 different values to the function. This is to save us from making a function for each form box.*/
var frm = document.forms[0]; //set a variable to stand for the form
var xpr, zlp, sub_2; //declare variables
if(total.value.indexOf(".") != -1){ //if the totalQTY's value has a period in it
xpr = total.value.indexOf("."); //xpr's value is now the first instance of a period in the totalQTY box
zlp = total.value.length; //zlp's value is an integer--which is the last character in the string (the string being the totalQTY box)

sub_2 = total.value.substring(xpr, zlp); //sub_2's value is now everything in between the number of xpr and the number of zlp

}
if(isNaN(total.value)){total.value="";} //if the totalQTY's value is Not a Number (NaN), make totalQTY's value nothing

total.value="$"+qty*price; //set the value of totalQTY to a dollar sign, plus the qty field times the price
}
</script>
Your form (example):

<form action="">
<td>
<!-- here we have named the qty field "qty_mealreaplaceshakevan." The event handler is onKeyDown and onKeyUp. The function called is calc() and the values are passed. In this case, the variable qty is the value of this text box, the value of the "total" variable is the text box below, named, "total_mealreplaceshakevan." The variable, "price" has been passed the value, "29.95" We will put this into currency format in the function. -->
<input name="qty_mealreplaceshakevan" size="5" onkeydown="calc(this.value,this.form.total_mealreplaceshakevan,'29.95');" onkeyup="calc(this.value,this.form.total_mealreplaceshakevan,'29.95');"></td>
<td>Meal Replacement Shake(vanilla)</td>
<td>$29.95</td>
<td><input name="total_mealreplaceshakevan" size="15"><br>

<!-- we have named this particular form box "qty_mealreplaceshakechoc." The event handlers are onKeyDown and onKeyUp (to process it more properly and accurately). Now, we are doing the same thing as above, but instead of referring to the first and second form boxes, we're now referring to these. We're also changing the price from 29.95 to 39.95. "this.form" is referring to the current form in which these boxes are held. -->
<input name="qty_mealreplaceshakechoc" type="text" size="5" onkeydown="calc(this.value,this.form.total_mealreplaceshakevchoc,'39.95');" onkeyup="calc(this.value,this.form.total_mealreplaceshakechoc,'39.95');"> </td>
<td>Meal Replacement Shake(chocolate)</td>
<td>$39.95</td>
<input name="total_mealreplaceshakechoc" type="text" size="15">
<br>
</form>

DarryBoy
04-23-2003, 07:01 PM
Jona I appreciate all the time you have given me. Its going to take a while for me to absorb what you have explained in your tutorial.

I'm so stressed because my boss wants me to complete the following form ready for today. It's now 1:50am in South Africa.

You have given me a calculation for the for the first selection that I thought would be easy to use to calculate the rest of the form.

Due to my deadline Jona please may I ask you to kindly help me. I will make sure that I learn JavaScript in the future.

The form in question is http://www.sunscreen4africa.co.za/sunscreen_orderform1.htm I haven't updated it yet.

Please, please help me.

Jona
04-23-2003, 07:07 PM
I'll work on it....

DarryBoy
04-23-2003, 07:26 PM
Thank you so much Jona. If there is anything you require please ask.

Jona
04-24-2003, 09:28 AM
Here's what I've come up with:

<html><head>
<script>
function calc(qty, total, box1, box2){
var price;
if(box1.checked){price=box1.value;}
if(box2.checked){price=box2.value;}
total.value=parseInt(qty)*price;
if(total.value.indexOf("$")){
total.value="$"+total.value+".00";}
}

function chk(box1, box2){
var count = 0;
if(box1.checked){count+=1;}
if(box2.checked){count+=1;}
if(count==2){alert("Check only one, please."); return false;}
}
</script>
</head><body>
<form name="qtyFrm">
<p>
<input type=text name="qty" onKeyDown="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2)" onKeyUp="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2)">
<br>
130:
<input type=checkbox name="c1" value="130" onclick=" calc(this.form.qty.value,this.form.qtyTotal,this,this.form.c2); return chk(this,this.form.c2);">
<br>
1300:
<input type=checkbox name="c2" value="1300" onclick="calc(this.form.qty.value,this.form.qtyTotal,this.form.c1,this); return chk(this.form.c1,this);">
<br>
<input type=text name="qtyTotal">
</p>
<p>
<input name="qty2" type="text" id="qty2" onKeyDown="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4)" onKeyUp="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4);">
<br>
130:
<input name="c3" type="checkbox" id="c3" value="130" onclick="calc(this.form.qty2.value,this.form.qtyTotal2,this,this.form.c4); return chk(this,this.form.c4);">
<br>
1300: <input name="c4" type="checkbox" id="c4" value="1300" onclick="calc(this.form.qty2.value,this.form.qtyTotal2,this.form.c3,this); return chk(this.form.c3,this);">
<br>
<input name="qtyTotal2" type="text" id="qtyTotal2">
</p>
<p>
<input type="text" name="textfield3" onkeydown="calc(this.value,this.form.textfield4,this.form.checkbox3,this.form.checkbox4)" onkeyup="calc(this.value,this.form.textfield4,this.form.checkbox3,this.form.checkbox4)">
<br>
130:
<input type="checkbox" name="checkbox3" value="180" onclick=" calc(this.form.textfield3.value,this.form.textfield4,this,this.form.checkbox4); return chk(this,this.form.checkbox4);">
<br>
1300:
<input type="checkbox" name="checkbox4" value="1800" onclick=" calc(this.form.textfield3.value,this.form.textfield4,this.form.checkbox3,this); return chk(this.form.checkbox3,this);">
<br>
<input type="text" name="textfield4">
<br>
</p>
</form></body></html>

DarryBoy
04-24-2003, 12:30 PM
Thank you so much Jona. Please may you email me darryboy10@hotmail.com I would like to share with you my fight in life for survival.

Jona
04-24-2003, 12:31 PM
Just email me: god_loves_07@yahoo.com ;)

DarryBoy
04-28-2003, 03:23 PM
Hi Jona

I have added 2 more additional fields (postalCost & totalCost) How do I add subtotal to the postalCost that has a Init Val of 30.00 and it will put the calculated value in the totalCost field?

<html><head>
<script>
function calc(qty, total, box1, box2){
var price;
if(box1.checked){price=box1.value;}
if(box2.checked){price=box2.value;}
subtotal=total.value=parseInt(qty)*price;
if(isNaN(total.value)){total.value="0";}
if(total.value.indexOf("R")){
total.value=""+total.value+".00";}
var a = document.qtyFrm.qtyTotal.value.split("R").join("");
b = document.qtyFrm.qtyTotal2.value.split("R").join("");
c = document.qtyFrm.qtyTotal3.value.split("R").join("");
all = parseFloat(a*1+b*1+c*1)
document.qtyFrm.subtotal.value=""+all+".00";
}

function chk(box1, box2){
var count = 0;
if(box1.checked){count+=1;}
if(box2.checked){count+=1;}
if(count==2){alert("Check only one, please."); return false;}
}
</script>
</head><body>
<form name="qtyFrm">
<p>
<input type=text name="qty" onKeyDown="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2)" onKeyUp="calc(this.value,this.form.qtyTotal,this.form.c1,this.form.c2)">
<br>
130:
<input type=checkbox name="c1" value="130" onClick=" calc(this.form.qty.value,this.form.qtyTotal,this,this.form.c2); return chk(this,this.form.c2);">
<br>
1300:
<input type=checkbox name="c2" value="1300" onClick="calc(this.form.qty.value,this.form.qtyTotal,this.form.c1,this); return chk(this.form.c1,this);">
<br>
<input type=text name="qtyTotal">
</p>
<p>
<input name="qty2" type="text" id="qty24" onKeyDown="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4)" onKeyUp="calc(this.value,this.form.qtyTotal2,this.form.c3,this.form.c4);">
<br>
130:
<input name="c3" type="checkbox" id="c3" value="130" onClick="calc(this.form.qty2.value,this.form.qtyTotal2,this,this.form.c4); return chk(this,this.form.c4);">
<br>
1300:
<input name="c4" type="checkbox" id="c4" value="1300" onClick="calc(this.form.qty2.value,this.form.qtyTotal2,this.form.c3,this); return chk(this.form.c3,this);">
<br>
<input name="qtyTotal2" type="text" id="qtyTotal23">
</p>
<p>
<input type="text" name="textfield3" onKeyDown="calc(this.value,this.form.qtyTotal3,this.form.checkbox3,this.form.checkbox4)" onKeyUp="calc(this.value,this.form.qtyTotal3,this.form.checkbox3,this.form.checkbox4)">
<br>
130:
<input type="checkbox" name="checkbox3" value="130" onClick=" calc(this.form.textfield3.value,this.form.qtyTotal3,this,this.form.checkbox4); return chk(this,this.form.checkbox4);">
<br>
1300:
<input type="checkbox" name="checkbox4" value="1300" onClick=" calc(this.form.textfield3.value,this.form.qtyTotal3,this.form.checkbox3,this); return chk(this.form.checkbox3,this);">
<br>
<input name="qtyTotal3" type="text" id="qtyTotal34">
</p>
<p>
<input name="subtotal" type="text" id="subtotal">
<br>
<input name="postalCost" type="text" id="postalCost" value="30.00">
<br>
<input name="totalCost" type="text" id="totalCost">
<br>
</p>
</form></body></html>

Jona
04-28-2003, 05:09 PM
all = parseFloat(a*1+b*1+c*1+document.qtyFrm.postalCost.value);

DarryBoy
04-28-2003, 06:04 PM
Thanx Jona but this only takes the int val of 30 and displays it next to the subtotal value. Run the code to see.

Jona
04-28-2003, 06:05 PM
all = parseFloat(a*1+b*1+c*1+document.qtyFrm.postalCost.value*1);

DarryBoy
04-28-2003, 06:36 PM
U r brilliant Jona. It shows 30 (the Int value) in the subtotal field when u enter a value in the qty field. How do I make this value not show but it must still add to the subtotal?

Jona
04-28-2003, 07:06 PM
You mean you want to take the whole textfield out? Just muliply it by thirty:

all = parseFloat(a*1+b*1+c*1+30);

Or, if you need it in your form, just change it from type="text" to type="hidden"

DarryBoy
04-28-2003, 08:04 PM
I want to keep the textfield. If you run the code and enter a value in the qty field 30 is shown in the subtotal field before the calculation. I don't want it shown. I only want the calculated value shown.

If you run the code you will follow.

DrDaMour
04-29-2003, 02:24 AM
you are paying him for this i hope, since you aren't even attempting to learn, and just having him do everything peice by piece.

DarryBoy
04-29-2003, 06:28 AM
You are right DrDaMour and I apologise. Jona I apologise. I'm very grateful for what you have shown me and will tackle JavaScript as best as I can.

Kind regards
Darryn

Jona
04-29-2003, 11:45 AM
First of all, this is a "free" forum, for free answers. If I wanted money, I would've asked for it. DrDaMour, the suggestion is a great idea, but if I wanted money or to get paid to answer questions, I would join the experts-exchange (http://experts-exchange) group.

Also, DrDaMour, I'd like you to remove your OutWar link from you signature, please. I have no authority to tell you this (or force this upon you in any way), but I'd like to tell you that I quite often see people post outwar links (on many forums), and outwar is not, in my opinion, anything other than a waste of time--and a way to frustrate people. Understand me, I'm not upset with you, I am just asking, as a friend, if you would please remove the link. Thank you. :)