...

View Full Version : Need to add a value to totalsum with a set of radio buttons



prem_amrit
06-16-2007, 12:31 PM
I have a somewhat peculiar problem.
I'm trying to add a value from a checked radio button.
The radio buttons has assigned values, but when including the code i get a NaN.

I have tried to figure out why a checked value of 1 or 0 would give me a NaN but hey, I'm just a n00b.

This is parts of the code. The part I'm having trouble with is bolded:


<script language="JavaScript">

function calculate(){
var totalsumma = 0;
if(!isNaN(parseInt(document.forms[0].artnr110.value))){
totalsum=totalsum + parseInt(document.forms[0].artnr110.value)*199;
document.forms[0].sum110.value = parseInt(document.forms[0].artnr110.value)*199;
}

if(!isNaN(parseInt(document.forms[0].extra.value))){
totalsum=totalsum + parseInt(document.forms[0].extra.checked)*50;
}

if(!isNaN(parseInt(document.forms[0].payment.value))){
totalsum=totalsum + parseInt(document.forms[0].payment.checked)*0;
}

document.forms[0].sum.value = totalsum + (60) + " :-";
}
</script>

<form name="order" action="order.asp" method="POST">

<INPUT TYPE="TEXT" SIZE="2" NAME="artnr110" id="artnr110" onChange="calculate()">
<INPUT TYPE="TEXT" SIZE="2" NAME="summa110" id="sum110">
<INPUT TYPE="TEXT" SIZE="3" NAME="sum" id="sum" width="20">

<INPUT NAME="betalning" type="radio" id="extra" onChange="calculate()" value="1">
<INPUT NAME="betalning" type="radio" id="payment" onChange="calculate()" value="1">


I don't know how many braincells got cooked in my brain trying to figure out why 1 or 0 becomes a NaN.
If i change the integer checked to value, then there is no problem. (Don't know if that was the correct syntax...)

Hope someone here can help me. It's not like I haven't tried...

\\Amrit

Philip M
06-16-2007, 03:22 PM
Quite a few problems here. For example, inconsistency of variable names totalsumma/totalsum.

Try this to move you forward, but there is still a lot to be done to make this useable.

<Script type = "text/Javascript">

function calculate() {
var totalsumma = 0;
var artvalue = document.order.artnr110.value; // avoid repetition

if (!isNaN(artvalue)) {
totalsumma = (parseInt(totalsumma + artvalue) * 199);
document.order.summa110.value = totalsumma;
}

if (document.order.betalning[0].checked) {
totalsumma = totalsumma * 50;
}

if (document.order.betalning[1].checked) {
totalsumma = totalsumma + 60;
}

document.forms[0].sum.value = totalsumma + " :-";
}
</script>

<FORM ame="order" action="order.asp" method="POST">

<INPUT TYPE="TEXT" SIZE="2" NAME="artnr110" id="artnr110" >
<INPUT TYPE="TEXT" SIZE="2" NAME="summa110" id="summa110">
<INPUT TYPE="TEXT" SIZE="3" NAME="sum" id="sum" width="20">

<INPUT NAME="betalning" type="radio" id="extra" value="1">
<INPUT NAME="betalning" type="radio" id="payment" value="1">
<INPUT TYPE ="button" name = "go" value = "Calculate" onclick = "calculate();"

</FORM>

prem_amrit
06-16-2007, 05:56 PM
Hi Philip. Thank you for helping me.
I love learning coding, but heck, sometimes the mind goes really crazy trying to figure things out.

Yes, the inconsistencies got in there because I was ignorant enough to try and make some variables into english. (my bad)

The script you wrote Philip worked great.

But. And there is always a but. (Gotta sit on something)

It seems the script only work for one var setting.
I have like 41 var that is set, but when I do I'm clearly doing something wrong because the way the script seems to work it can't be just duplicated, line after line (Seems so, or I have missed something)



<Script type = "text/Javascript">

function calculate() {
var totalsumma = 0;
var artvalue01 = document.order.artnr110.value; // avoid repetition
var artvalue02 = document.order.artnr51.value; // incremental upto 41

if(!isNaN(artvalue01)) {
totalsumma = (parseInt(totalsumma + artvalue01) * 199);
document.order.summa110.value = totalsumma;
}

if(!isNaN(artvalue02)) {
totalsumma = (parseInt(totalsumma + artvalue02) * 199);
document.order.summa51.value = totalsumma;
}

document.order.summa.value = totalsumma + " :-";
}
</script>


When I replicate the code like this, from artvalue01 -> artvalue41 it seems as though the summa51 is multiplied with summa110 and so forth making the totalsumma like huuuge.

Guessing there is another approach when dealing with multiple var's.

Any help greatly appreciated.

\\Amrit

Philip M
06-16-2007, 06:45 PM
Well, yes, because each time you multiply the value of totalsumma by 199!


function calculate() {
var totalsumma = 0;
var itemsumma = 0;
var artvalue01 = document.order.artnr110.value; // avoid repetition
var artvalue02 = document.order.artnr51.value; // incremental upto 41

if(!isNaN(artvalue01)) {
itemsumma = (parseInt(artvalue01) * 199); // value for this item
document.order.summa110.value = itemsumma;
totalsumma = totalsumma + itemsumma; // accumulated total
}

if(!isNaN(artvalue02)) {
itemsumma = (parseInt(artvalue02) * 199);
document.order.summa51.value = itemsumma;
totalsumma = totalsumma + itemsumma;

}

If you are trying to manipulate 41 items (variables) then you would do best to set up an array to hold the calculated values.

prem_amrit
06-17-2007, 12:58 AM
Thanks again Philip.

Tried your solution. I got a NaN in every summa except for the one field I filled in. And the same happened when clicking either of my radio buttons.

So i went ahead and tried an array.
But instead, I get nothing. No calculation is performed, or displayed.
Though the field "summa" atleast shows 0 :- instead of NaN, and also the radio buttons work and the correct value is presented in "summa" when selecting either button.

This is as far as I have written the code now. (Learning alot here. )

As you can see I want the calculate() to be performed when entering a number into any of the artvaluefields. (Or is it possible to create a calculate function for each artvalue?)
Tried looking around the net for good information on using calculate but no luck there...

Also, I tried these strings in my array "document.order.artnr110.value", but that didn't help. So I've tried around for quite a bit now... 2 am in the morning now so time to sleep some now...



<Script type = "text/Javascript">

function calculate() {
var totalsumma = 0;
var itemsumma = 0;
var artvalue = new Array ();
artvalue[0] = "artnr110";
artvalue[1] = "artnr51";
artvalue[2] = "artnr40";

if(!isNaN(artvalue[0])) {
itemsumma = (parseInt(artvalue[0]) * 199);
document.order.summa110.value = itemsumma;
totalsumma = totalsumma + itemsumma;
}
if(!isNaN(artvalue[1])) {
itemsumma = (parseInt(artvalue[1]) * 199);
document.order.summa51.value = itemsumma;
totalsumma = totalsumma + itemsumma;
}

if(!isNaN(artvalue[2])) {
itemsumma = (parseInt(artvalue[2]) * 222);
document.order.summa40.value = itemsumma;
totalsumma = totalsumma + itemsumma;
}

if (document.order.betalning[0].checked) {
totalsumma = totalsumma + 110;
}

if (document.order.betalning[1].checked) {
totalsumma = totalsumma + 60;
}

document.order.summa.value = totalsumma + " :-";
}
</script>

<form name="order" action="order.asp" method="post">

<INPUT TYPE="TEXT" SIZE="2" NAME="artnr110" id="artnr110" onChange="calculate()">
<INPUT TYPE="TEXT" SIZE="2" NAME="summa110" id=summa110>
<INPUT TYPE="TEXT" SIZE="2" NAME="artnr51" id="artnr51" onChange="calculate()">
<INPUT TYPE="TEXT" SIZE="2" NAME="summa51" id=summa51>
<INPUT TYPE="TEXT" SIZE="2" NAME="artnr40" id="artnr40" onChange="calculate()">
<INPUT TYPE="TEXT" SIZE="2" NAME="summa40" id=summa40>

<INPUT NAME="betalning" type="radio" id="extra" value="1">
<INPUT NAME="betalning" type="radio" id="payment" value="1">

</FORM>


Thank you in advance. Words can't really convey the appreciation for recieving help with this mindtwisting script, but really. Thank your for trying to help me Philip.

Sincerly

\\Amrit

Philip M
06-17-2007, 07:49 AM
I am afraid that I am not at all clear as to what it is you are trying to achieve. I think you need to have a clearer idea of how your script is going to work, i.e inputs and outputs.

var artvalue = new Array ();
artvalue[0] = "artnr110";
artvalue[1] = "artnr51";
artvalue[2] = "artnr40";

if(!isNaN(artvalue[0])) { itemsumma = (parseInt(artvalue[0]) * 199);


the array artvalue ought to contain the values (prices??) of the various articles. It will make it a lot simpler if artvalue[110] contains the fixed value for article number 110, artvalue[40] contains the value for article number 40 and so on.

Then another array is artquantity() which contains the number of items as input by the user (if I understand what you are doing correctly).

Your array of article names should be artname() with the elements corresponding to the values in artvalue.

So, to summarise, three arrays containing name, value, quantity.
And a fourth array to hold the result, i.e. value x quantity.

The in essense you need a for loop something like this:-

for (i=1, i = artvalue.length +1, i++) {

if (!isNaN(artquantity[i])) { // need something to trap if user entry isNaN
result[i] = (parseInt(artvalue[i]) * artquantity[i]);
}
}

prem_amrit
06-17-2007, 11:48 AM
Hi Philip.

Glad you´re helping me.
Feels like a crashcourse in javascripting, but I'm starting to get the hang of it.

Perhaps a clarification on what the script should be doing.
You're actually basically right on target.
www.viio.se/order.asp
That's the page I'm doing. (It's in swedish though)

artvalue = Pris
artname = Art.nr
artquantity = Antal
sum = Summa
totalsumma = Total

The radio button extra and payment is at the bottom of the page, obviously.

When someone enters a value into Antal the script will automatically multiply that number with Pris and showing the result in corresponding Summa and also the added amount in Total
The customer is then to click either radio button and the value of that is added to Total.

That is what I want it to do and I'm learning a lot doing this. Thanks for helping me.

I see why it is easier to have the arrays artvalue, artname & artquantity.
But I don't get how the array artquantity should be.
I'm guessing this way is wrong.
var artquantity = new Array ();
artquantity[0] = "1";

Slowly slowly I'm getting there. Guessing everyone began as a n00b.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum