Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need to add a value to totalsum with a set of radio buttons

    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:
    Code:
    <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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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>
    Last edited by Philip M; 06-16-2007 at 03:25 PM.

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

    Code:
    <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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.
    Last edited by Philip M; 06-16-2007 at 06:47 PM.

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

    Code:
    <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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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]);
    }
    }

  • #7
    New Coder
    Join Date
    Jun 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •