krs10_s Posted March 9, 2009 Share Posted March 9, 2009 i need to add up all the values selected from a number of dropdown menues (number unknown). here's some code i got but it's not working! print "<tr><td style='border:0px solid black;'>".$m." <input type='text' name='box_".$m."'size='10'><b>R</b></input> <select name='randi_".$m."'>"; for($k=0;$k<=5000;$k+=100) { print "<option value='$k'>$k</option>"; } print "</select></td></tr>"; } and then the next page print "number selected: ".$_POST['income']."<p/>"; for ($m=1;$m<=$_PoST['income'];$m++) { $inc='box_1'; ; print "Value $m: ".$_POST[$inc]."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/148569-help-with-forms-need-to-add-up-lots-of-values-from-a-drop-down-menu/ Share on other sites More sharing options...
Adam Posted March 9, 2009 Share Posted March 9, 2009 Give the drop downs you wish to add up a prefix to the name then loop through the $_POST array and look for that prefix... So you'd have your menus / inputs: <select name="countNumber1">...</select> <select name="countNumber2">...</select> And then loop through them like so: $total = 0; foreach ($_POST as $key => $x) { if (substr($key, 0, 5) == 'count') { $total += (int) $x; } } echo $total; You don't have to use 'count' obviously.. Adam Quote Link to comment https://forums.phpfreaks.com/topic/148569-help-with-forms-need-to-add-up-lots-of-values-from-a-drop-down-menu/#findComment-780154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.