Forum Moderators: coopster

Message Too Old, No Replies

Item from Array Drop Down

         

field4000

1:08 pm on Dec 1, 2007 (gmt 0)

10+ Year Member



Hey all,

I have a drop down which is generated by an array with a list of numbers.

I am trying to use the selected number for a calculation: $hours * 10. However, I am not sure how to pull the selected number out.

Below is my code:

<select class="select" id="hours" name="hours">
<option value="0" selected="selected">Hours</option>

<?

foreach ($hours as $key => $value)
{
echo "<option value='$key'>$value</option>\n";
}

?>
</select>

Any help with this would be greatly appreciated.

Cheers in advance.

PHP_Chimp

7:23 pm on Dec 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<select class="select" id="hours" name="hours">
<option value="0" selected="selected">Hours</option>

Assuming that you are using post for the form you will get
$_POST['hours'] = 0; in this case. As there is only 1 option.

The key for the $_POST array will be the id/name. The value will either be the value if you specify one, or if you dont then it will be the contents of that option.
So if you had -


<select class="select" id="hours" name="hours">
<option value="0" selected="selected">0 Hours</option>
<option value="10" selected="selected">10 Hours</option>
<option value="15" selected="selected">15 Hours</option>
<option value="500" selected="selected">500 Hours</option>

And someone selects that they worked for 500 hours then you would get $_POST['hours'] = 500;