Jump to content

help with forms! need to add up lots of values from a drop down menu


krs10_s

Recommended Posts

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/>";
		}

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

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.