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.
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";}
}
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";
}
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. :)
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.