View Full Version : Resetting drop down menu values

03-27-2004, 03:24 AM
After the user clicks a button, I would like to reset the chosen value of a drop down menu. For example, if I have the following drop down menus:
for (i =0; i<7; i++){
<span class="style1">Out</span>
<td width=""><select id="stop<? echo $i ?>" name= "stop<? echo $i ?>">
<option value="0" default></option>
<option value="0">12:00 AM</option>
<option value="0.5">12:30 AM</option>
<option value="1">1:00 AM </option>


How can I change the option value from 0 to 12:00 AM so that A12:00 AM gets saved to the database (and not the 0)? Is it possible in php so that I can do something like this:

stop1 option value = stop1 option text

I have 14 drop down menus with each having 24 options, so if I could something like that statement it would be very help.

Please note, for a good reason (and I dont want to get into because its just a long story), I cannot recode the above option values to the following (if I could, I wouldnt be posting this):

<option value="12:00 AM">12:00 AM</option>
<option value="12:30 AM">12:30 AM</option>
<option value="1:00 AM">1:00 AM </option>

Any help is greatly appreciated! =)

03-27-2004, 09:55 AM
3 options:
javascript, but since this is clientsided you could then just as well change the values (just hypothising about your reasons to no use the desires values)

server side scripting --> when you build the dropdown, use 0,1,2,3,4,... as values. Then you create an array like

$drp_realvalues= array('12:00 AM', '12:30 AM', '1:00 AM');

(note : if the dropdown is build dynamically, then you can create this inside the same loop as you create the dropdown)

Now, when the form is posted, you go

$realvalue = array('dummy');
for (i =0; i<7; i++){
$realvalue[] = $drp_realvalues[$_POST['stop' . $i]] ;

$realvalue[1] now contains the same value as the label of dropdown 1

Use a db Store the different times inside a db-table. Then use the primary key value of the record as value inside the dropdown
--> additional advantage, you can create the records randomly (or assign the pk values yourself in a random order)

to get the value (if you still realy need it) then you go

sql="select timelabel from timetable where timeID=" . $_POST['stop' . $i] ;

03-27-2004, 10:37 PM
if I understand your problem correctly wouldn't it be possible to remove the value for the default select and have a pice of code on the submit page like this?

if($stop1 == "0"){
// Add 12.00 AM to the database

03-28-2004, 10:22 AM
I have 14 drop down menus with each having 24 options,

So you would need at least 24 if's and 24 different ''// Add 12.00 AM to the database'' + a loop for the 14 dropdowns, and that is then even just if they all have the same values. Otherwise, you'll need more then 24 if's then need to be evaluated all 14 times.
I don't even see how you would tackle the dropdown-identification in your code.

That's why i thought it would be better to set up one array. i strongly suspect all 14 dropdowns have the same values. But even if that isn't true, you can still build 1 array wth the union off all values.

Processing the form for all 14 dropdown then takes 5 lines

foreach($_POST as $var=>$val){
if (substr($var,0,4)=='stop'){
$realvalue[] = $realvalue + $drplabel;

after wich you have a nice array with the labels and the drpnumbers as keys. And you could even have 100 dropdowns, the code wouldn't be needing any change.
If the value needs to ne processed or inserted, then you can do that inside the loop istead of building the array.
It will be much faster because i only need one evaluation for each formfield to filter out the dropdowns and then it's just a lookup from the array.

But i personally would use the db-approach becasue you then don't need any conversion. Just selec PK-value (used as option-value) and label when building the array, and then insert the PK-value when processing, also in a foreach-loop.