Autoselect Drop Down Values

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Autoselect Drop Down Values

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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>
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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? :)
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Post by ibanez270dx »

Thanks guys! It works flawlessly now!
Post Reply