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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.