Jump to content

On-the-fly sum from multiple drop down boxes


Hyperjase

Recommended Posts

Hi all,

 

I'm trying to create a way of showing the sum total of a number of drop down boxes. The number of drop down boxes is dynamic, created by two variables - one being how many cupcakes and how many toppings are selected.  It then creates drop downs for each selected topping and the value is the number of cupcakes chosen.  Here is the code I have:

 

<?php
foreach ($_SESSION['topping'] as $value) {
    echo "<tr><td width='30%'>Topping</td><td width='50%'>$value</td><td width='20%'><select name='notopping[$value]'>";
$counter = 1;
while ( $counter <= $_SESSION['cupcake'] ) {
  echo '<option value='.$counter.'>'.$counter.'</option>';
  $counter++;
}
echo "</select</td></tr>";
}
echo "<tr><td colspan='3' align='center'>All must add up to ".$_SESSION['cupcake']."</td></tr>";
if (count($_SESSION['swtopping']) > 0) {
?>
<tr>
<td colspan="3">
<br /><br />Please select how many of each type of sweetie topping you would like<br />
</td>
</tr>
<?php
foreach ($_SESSION['swtopping'] as $value) {
    echo "<tr><td width='30%'>Sweetie topping</td><td width='50%'>$value</td><td width='20%'><select name='noswtopping[$value]'>";
$counter = 1;
while ( $counter <= $_SESSION['cupcake'] ) {
  echo '<option name="noswtopping" value="'.$counter.'">'.$counter.'</option>';
  $counter++;
}
echo "</select></td></tr>";
}
echo "<tr><td colspan='3' align='center'>All must add up to ".$_SESSION['cupcake']."</td></tr>";
}
?>

I want a way when each drop down is changed it updates the total number selected.  Also is there a way of not allowing the submit button to be pressed until the value of all the drop down boxes match the total number of cupcakes.

 

Is this possible?

 

Thanks,

 

Jason

This is exactly what I need, I'm currently trying this code but it doesn't want to work, just no adding it up.

 

http://www.mcfedries.com/javascript/ordertotals.asp

 

When you select the drop down and a value, it automatically updates the total value.

 

Thanks,

 

Jason

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.