Page 1 of 1

Autoselect Drop Down Values

Posted: Wed Aug 16, 2006 10:15 am
by ibanez270dx
Hi,
Is it possible to have a certain value of a drop-down box be selected automatically? For example, in my logging system, everytime someone wants to edit an entry, the date gets reset (I have month, day, and year drop down boxes). All of those boxes are hard coded... is it even possible to do what I want to do?

Thanks,
- Jeff

Posted: Wed Aug 16, 2006 10:18 am
by feyd
This isn't really PHP..

Code: Select all

<select name="foo">
  <option value="something">foo!</option>
  <option value="something" selected="selected">foo?</option>
  <option value="something">foo^</option>
</select>

Posted: Wed Aug 16, 2006 10:20 am
by JayBird
Yes, just use "selected" like so

Code: Select all

<select name="foo">
  <option value="else">something</option>
  <option value="devnetwork" selected="selected">php</option>
  <option value="nice">isntthis</option>
</select>

Posted: Wed Aug 16, 2006 10:37 am
by ibanez270dx
Thanks - but there is a problem with it. My Select Boxes are hard coded and I need the PHP to decide which option is selected... here is my code: Currently, it autoselects "No Crew" every time....

Code: Select all

if($dwntime_type == "unscheduled")
	{
	 $unscheduled_s = "selected";
	}
if($dwntime_type == "progressive")
	{
	 $progressive_s = "selected";
	}
if($dwntime_type == "No Crew")
	{
	 $nc_s = "selected";
	}

HTML:

Code: Select all

<select tabindex=1 name="dwntime_type" size="1" width="107">
	<option value="scheduled">Scheduled</option>
	<option value="unscheduled" selected="<? echo $unscheduled_s; ?>">Unscheduled</option>
	<option value="progressive" selected="<? echo $progressive_s; ?>">Progressive</option>
	<option value="nc" selected="<? echo $nc_s; ?>">No Crew</option>
 </select>
Anyone know what I'm doing wrong or what I can do to fix this?

Thanks,
- Jeff

Posted: Wed Aug 16, 2006 10:39 am
by JayBird
Need to see more code, specifically, how are you setting $dwntime_type.

Also, when you have run the code, view source and post the genrated HTML

Posted: Wed Aug 16, 2006 10:40 am
by feyd
it's because you have the selected attribute in all of them.

Code: Select all

<select tabindex=1 name="dwntime_type" size="1" width="107">
   <option value="scheduled">Scheduled</option>
   <option value="unscheduled" <?php echo $unscheduled_s; ?>>Unscheduled</option>
   <option value="progressive" <?php echo $progressive_s; ?>>Progressive</option>
   <option value="nc" <?php echo $nc_s; ?>>No Crew</option>
 </select>
Can you guess what you're supposed to put in each now? :)

Posted: Wed Aug 16, 2006 10:53 am
by ibanez270dx
Thanks guys! It works flawlessly now!