...

View Full Version : Error getting correct value



etkam
07-19-2012, 05:54 AM
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
:confused::confused::confused:



<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" />

MarPlo
07-19-2012, 08:12 AM
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:

p1 = field.options[field.selectedIndex].value * 1;
r1 = field.options[field.selectedIndex].value * 1;

etkam
07-23-2012, 05:43 AM
hi MarPlo

TQ that solve my case

xoxo~



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum