Jump to content

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


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

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.