So I have this form I created in PHP, to get a general estimate of the
cost of my product... right now i have it so whatever you select, be
it in a dropdown menu or checkbox, your total is displayed at the
bottom, in textboxes, by clicking submit. I'd like it so the form
dynamically changes these totals depending on the value of the
dropdowns/checkboxes so I can remove the submit button completely if
that makes any sense... anybody have any suggestions, or is there a
tutorial out there that shows how to do this?
Thanks
truck
And another how to example. One thing to watch for is the string to
number conversion in JavaScript.
parseInt and parseDouble are two useful functions to deal with this.
<html>
<head>
<script>
function updateTotals(){
totalBX = document.getElementById('totals');
totalBX.value = addEmUp();
}
function addEmUp(){
priceOfShirt = 5; //cheap ey?
totals = 0;
garmQTY = document.getElementById('garmentquantity');
prntLocation = document.getElementById('imprintlocations');
prntLocation1 = document.getElementById('imprintlocation1');
garmColors = document.getElementById('colors1');
totals += priceOfShirt * parseInt(garmQTY.value);
totals += (parseInt(prntLocation.value) * 2) *
parseInt(garmQTY.value); //$2 bucks per location per shirt
switch(prntLocation1.value){
case 'full_front': totals += parseInt(garmQTY.value) * 3; //$3 bucks
for this option per shirt
break;
case 'front_left_chest': totals += parseInt(garmQTY.value) * 2; //$2
bucks for this one option per shirt
break;
default: totals += parseInt(garmQTY.value) * 1; //all others are a
buck per shirt
break;
}
switch(garmColors.value){
case "1_color": totals += parseInt(garmQTY.value) * 1;
break;
case "2_color": totals += parseInt(garmQTY.value) * 2;
break;
case "3_color": totals += parseInt(garmQTY.value) * 3;
break;
case "full_color": totals += parseInt(garmQTY.value) * 4;
break;
}
return totals;
}
</script>
</head>
<body onload="updateTotals()">
<form>
Garment Quantity
<select id="garmentquantity" style="width: 200px; margin: 3px;"
onchange="updateTotals()">
<option value="10" SELECTED >10</option>
<option value="20" >20</option>
<option value="30" >30</option>
<option value="40" >40</option>
<option value="50" >50</option>
<option value="60" >60</option>
<option value="70" >70</option>
<option value="80" >80</option>
<option value="90" >90</option>
<option value="100" >100</option>
</select>
<hr />
# of Imprint Locations
<select id="imprintlocations" style="width: 200px; margin: 3px;"
onchange="updateTotals()">
<option value="1" SELECTED>1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
</select>
<hr />
Imprint Location
<select id="imprintlocation1" style="width: 200px; margin: 3px;"
onchange="updateTotals()">
<option value="full_front" SELECTED>Full Front</option>
<option value="front_left_chest" >Left Chest</option>
<option value="front_center_chest" >Center Chest</option>
<option value="front_right_chest" >Above Pocket</option>
</select>
<hr />
# of Colors
<select id="colors1" style="width: 200px; margin: 3px;"
onchange="updateTotals()">
<option value="1_color" SELECTED>1 Color</option>
<option value="2_color" >2 Color</option>
<option value="3_color" >3 Color</option>
<option value="full_color" >Full Color</option>
</select>
<hr />
Total: <input type="text" id="totals" value="0" />
</form>
</body>
</html>