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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Error getting correct value

    There's problem in getting the correct sum value for quantity field

    example input
    checkSelection1 = 1
    checkSelection2 = 0
    checkSelection3 = 3

    output is : 103 instead of 4
    i don't know what is the problem since i can get my total correct


    Code:
    <script>
    var d50 = 0.50;
    var d60 = 0.40;
    var d70 = 0.30;
    
    var p1 = 0;
    var p2 = 0;
    var p3 = 0;
    
    var r1 = 0;
    var r2 = 0;
    var r3 = 0;
    
    var totprod =0;
    var quantity =0;
    
    function checkSelection1(field)
    {
    	p1=field.options[field.selectedIndex].value;
    	r1=field.options[field.selectedIndex].value;
    	if (p1 == 0)  {p1 = 304 * 0;}
    	if (p1 == 1)  {p1 = 304 * d50;}
    	if (p1 == 2)  {p1 = (304 * d60) * 2;}
    	if (p1 == 3)  {p1 = (304 * d70) * 3;}
    	if (p1.toString() == "NaN")  {p1 = 0;}
    
    	document.getElementById('mySelection1').value= p1.toFixed(2);
    	document.getElementById('quantity').value= r1+r2+r3;
    	document.getElementById('totprice').value= (p1+p2+p3+p4+p5+p6+p7+p8).toFixed(2);
    }
    
    function checkSelection2(field)
    {
    	p2=field.options[field.selectedIndex].value;
    	r2=field.options[field.selectedIndex].value;
    	if (p2 == 0)  p2 = 288 * 0;
    	if (p2 == 1)  p2 = 288 * d50;
    	if (p2 == 2)  p2 = (288 * d60) * 2;
    	if (p2 == 3)  p2 = (288 * d70) * 3;
    	if (p2.toString() == "NaN")  p1 = 0;
    
    	document.getElementById('mySelection2').value= p2.toFixed(2);
    	document.getElementById('quantity').value= r1+r2+r3;
    	document.getElementById('totprice').value= (p1+p2+p3+p4+p5+p6+p7+p8).toFixed(2);
    }
    
    function checkSelection3(field)
    {
    	p3=field.options[field.selectedIndex].value;
    	r3=field.options[field.selectedIndex].value;
    
    	if (p3 == 0)  p3 = 228 * 0;
    	if (p3 == 1)  p3 = 228 * d50;
    	if (p3 == 2)  p3 = (228 * d60) * 2;
    	if (p3 == 3)  p3 = (228 * d70) * 3;
    	if (p3.toString() == "NaN")  p1 = 0;
    
    	document.getElementById('mySelection3').value= p3.toFixed(2);
    	document.getElementById('quantity').value= r1+r2+r3;
    	document.getElementById('totprice').value= (p1+p2+p3+p4+p5+p6+p7+p8).toFixed(2);
    }
    
    </script>
    
    <select name="LL" onchange="checkSelection1(this)">
    	<option value="0">Select</option>
    	<option value="1"<?php if($LL == "1") print "selected"; ?>>@50% Off</option>
    	<option value="2"<?php if($LL == "2") print "selected"; ?>>@60% Off</option>
    	<option value="3"<?php if($LL == "3") print "selected"; ?>>@70% Off</option>
    </select>
     <input type="text" name="mySelection1" id="mySelection1" size="15" readonly="readonly" />
     <select name="LS" onchange="checkSelection2(this)">
    	<option value=0"">Select</option>
    	<option value="1"<?php if($LS == "1") print "selected"; ?>>@50% Off</option>
    	<option value="2"<?php if($LS == "2") print "selected"; ?>>@60% Off</option>
    	<option value="3"<?php if($LS == "3") print "selected"; ?>>@70% Off</option>
    </select>
     <input type="text" name="mySelection2" id="mySelection2" size="15" readonly="readonly"/>
     <select name="RL" onchange="checkSelection3(this)">
    	<option value="0">Select</option>
    	<option value="1"<?php if($RL == "1") print "selected"; ?>>@50% Off</option>
    	<option value="2"<?php if($RL == "2") print "selected"; ?>>@60% Off</option>
    	<option value="3"<?php if($RL == "3") print "selected"; ?>>@70% Off</option>
    </select>
     <input type="text" name="mySelection3" id="mySelection3" size="15" readonly="readonly"/>
     Quantity : <input type="text" name="quantity" id="quantity" />
     Total Payment :<input name="totprice" type="text" id="totprice" size="15" />

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hi,
    JavaScript treats data from form fields as strings, so, the "+" will join strings.
    The values should be converted to numbers, simply multiplying by 1 .
    Example:
    Code:
    p1 = field.options[field.selectedIndex].value * 1;
    r1 = field.options[field.selectedIndex].value * 1;

  • Users who have thanked MarPlo for this post:

    etkam (07-23-2012)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hi MarPlo

    TQ that solve my case

    xoxo~


  •  

    Tags for this Thread

    Posting Permissions

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