I am having some limited success with a shopping cart display I am designing.
The aim is to enter your quantity in the first field (which will only accept integers) and as the number grows the price per item changes (volume based pricing) and the total is claculated. The prices are supplied by php and looped for each product. The price to volume information will be different for each product. There are 4 price bands with a lower and upper value.
I am getting the price calculated for the first price band but it never seems to progress to the next price band.
I am relatively new to javascript and this has been my most ambitios asking of it. Im I heading in the right direction? Is there anything I am missing?
The code is below:
[code]
<script language=”JavaScript”>
function calculate(total, price, quantity, price_1, price_2, price_3, price_4, lower_1, upper_1, lower_2, upper_2, lower_3, upper_3, lower_4, upper_4)
{
var re = new RegExp(/^d+$/); // only allow +ve integers
var m = re.exec(quantity.value);
if (m == null) { // reset fields if not integer
quantity.value = “”;
total.value = “”;
} else {
// the pricing bands
if(quantity.value >= lower_1.value && quantity.value <= upper_1.value){
price.value = price_1.value;
}else if(quantity.value >= lower_2.value && quantity.value <= upper_2.value){
price.value = price_2.value;
}else if(quantity.value >= lower_3.value && quantity.value <= upper_3.value){
price.value = price_3.value;
}else if(quantity.value >= lower_4.value && quantity.value <= upper_4.value){
price.value = price_4.value;
}
total.value = (price.value)*(quantity.value); // the calc
}
document.frmCart.total.value = price.value;
}
</script>
<form name=”frmCart” method=”get”>
<?php
$num = 0;
while($row = mysql_fetch_array($result)) // loop for each product
{
$num++; // Increment for each product
?>
<input type=”text” name=”quantity_<?=$num?>” size=”5″ onKeyUp=”calculate(document.frmCart.total_<?=$num?>, document.frmCart.price_<?=$num?>, document.frmCart.quantity_<?=$num?>, document.frmCart.price_1_<?=$num?>, document.frmCart.price_2_<?=$num?>, document.frmCart.price_3_<?=$num?>, document.frmCart.price_4_<?=$num?>, document.frmCart.quant_1_lower_<?=$num?>, document.frmCart.quant_1_upper_<?=$num?>, document.frmCart.quant_2_lower_<?=$num?>, document.frmCart.quant_2_upper_<?=$num?>, document.frmCart.quant_3_lower_<?=$num?>, document.frmCart.quant_3_upper_<?=$num?>, document.frmCart.quant_4_lower_<?=$num?>, document.frmCart.quant_4_upper_<?=$num?>)”>
<input class=”quantities” type=”hidden” name=”price_1_1″ value=”10.00″ size=”5″ >
<input class=”quantities” type=”hidden” name=”price_2_1″ value=”20.00″ size=”5″ >
<input class=”quantities” type=”hidden” name=”price_3_1″ value=”30.00″ size=”5″ >
<input class=”quantities” type=”hidden” name=”price_4_1″ value=”50.00″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_1_lower_1″ value=”1″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_1_upper_1″ value=”50″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_2_lower_1″ value=”51″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_2_upper_1″ value=”1000″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_3_lower_1″ value=”1001″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_3_upper_1″ value=”5000″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_4_lower_1″ value=”5001″ size=”5″ >
<input class=”quantities” type=”hidden” name=”quant_4_upper_1″ value=”10000″ size=”5″ >
$<input class=”quantities” readonly type=”text” name=”price_1″ size=”5″ >
$<input class=”quantities” readonly type=”text” name=”total_<?=$num?>” size=”5″ >
</form>
<?php
}
?>