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 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Form Calculations

    This code does not want to add the text box to my calculations. I also tried to parseFloat() the tDelivery but then it gives a NaN, I also tried parseInt() same Nan then With the exact code below it encapsulates the two totals printing eg: R150 R35.
    All the other boxes works perfectly just the last one to add the delivery.

    Code:
    // Function to calculate order value 
    function calculateValue(orders) {
    	var orderValue = 0;
     
    	// Run through all the product fields
    	for(var i = 0; i < orders.elements.length; ++i) {
     
    	// Get the current field
    	var formField = orders.elements[i];
     
    	// Get the fields name
    	var formName = formField.name;
    	
    	// Is it a "product" field?
            if (formName.substring(0,4) == "prod") {
     
    	// Items price extracted from name
    	var itemPrice = parseFloat(formName.substring(formName.lastIndexOf("_") + 1));
     
    	// Get the Quantity
    	var itemQuantity = parseInt(formField.value);
     
    	// Update the OrderValue
    	if(itemQuantity >= 0) {
    	orderValue += itemQuantity * itemPrice;
    	}
    	}
        }
    	
    	// Display the totals
        orders.totalExcl.value = "R " + orderValue.toLocaleString();
        
        // function to calculate VAT at 15% as required and total inclusive.
    	function calcTotals(oValue) {
    	var vat = oValue * .15;
    	var totalIncluding = oValue + vat;
    	var tDelivery = orders.delivery.value;
    	var theTotalOrder = totalIncluding + tDelivery;
    	orders.vat.value = "R " + vat.toLocaleString();
    	orders.totalIncl.value = "R " + totalIncluding.toLocaleString();
    	orders.totalOrder.value = "R " + theTotalOrder.toLocaleString();
    	}
    	return calcTotals(orderValue);
    }
    Code:
    <!-- Row 10, Col 1 Order Value exluding Vat -->
    <tr><td colspan="2" align="right">Order Value Excluding Vat:</td>
    
    <!-- Row 10, Col 2 Order Value exluding Vat Box-->
    <td colspan="1" align="left">
    <input name="totalExcl" type="text" style="margin-left: 60px" size="10" onfocus="this.form.elements[0].focus()" />
    </td></tr>
    
    <!-- Row 11, Col 1 Vat of 15% -->
    <tr><td colspan="2" align="right">Vat calculated at 15%:</td>
    
    <!-- Row 11, Col 2 Vat Value Box-->
    <td colspan="1" align="left"><input name="vat" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
    </td></tr>
    
    <!-- Row 12, Col 1 Total Order Value including Vat -->
    <tr><td colspan="2" align="right">Order Value Including Vat:</td>
    
    <!-- Row 12, Col 2 otal Order Value including Vat Box-->
    <td colspan="1" align="left"><input name="totalIncl" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
    </td></tr>
    
    <!-- Row 13, Col 1 Order Value -->
    <tr><td colspan="2" align="right">Delivery Options: 
    <input type="radio" name="sapo" value="35" onclick="delivery.value='R ' + this.value" /> R35 - SA Post Office 
    <input type="radio" name="sapo" value="80" onclick="delivery.value='R ' + this.value" /> R80 - Speed Services 
    <input type="radio" name="sapo" value="150" onclick="delivery.value='R ' + this.value" /> R150 - Courier Services 
    </td>
    
    <!-- Row 13, Col 2 Order Value Box-->
    <td colspan="1" align="left"><input name="delivery" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
    </td></tr>
    
    <!-- Row 14, Col 1 Total Order Value including Vat -->
    <tr><td colspan="2" align="right"><strong>Total Order Value:</strong></td>
    
    <!-- Row 14, Col 2 otal Order Value including Vat Box-->
    <td colspan="1" align="left"><input name="totalOrder" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
    </td></tr>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    *WHICH* "text box"?????

    If you mean "delivery", then of course it won't work right.

    delivery.value is going to be "R 35" or something like that, and you can't convert that to a number.

    Well, you can, but you have to strip off the "R" and the space first.

    Possibly like this:
    Code:
    	var tDelivery = Number( orders.delivery.value.substring(2) );
    A better solution, I think, would be to ignore delivery.value and go get the number from the set of radio buttons, directly. But either could work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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