Jump to content


Photo

Autoselect Drop Down Values


  • Please log in to reply
4 replies to this topic

#1 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 16 August 2006 - 03:12 PM

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

#2 Jak

Jak
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 16 August 2006 - 03:19 PM

Yeah, i assume your aware of the "selected" attribue of option tags? Anyway, whichever option you want to be the default you just need to add that attribute like this:

<option value="1" selected="selected">Test</option>

So if your date selection options (1 - 31) were generated by a loop you just need to thow in an if to check if the number your on matches todays date, and if it does just add that attribute.
Jack

#3 ibanez270dx

ibanez270dx
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 16 August 2006 - 03:41 PM

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....

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


HTML:
 <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

#4 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 16 August 2006 - 03:51 PM

Did you try looking to your source. Maybe in none of them there is selected="selected" option. This means there is a logical problem in your php code. Maybe that's a simple html problem. Try using:
if($dwntime_type == "unscheduled")	
{
 $unscheduled_s = "selected=\"selected\"";
}
if($dwntime_type == "progressive")
{
 $progressive_s ="selected=\"selected\"";
}
if($dwntime_type == "No Crew")
{
 $nc_s = "selected=\"selected\"";
}
[code]
it's worth a try.[/code]

Roy Simkes
Yet Another Parkyeri Developer

#5 Jak

Jak
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 17 August 2006 - 10:54 AM

Here is (what i consider to be) a simpler, more elegant and easier to manage solution to your problem:

<?php
$dwntime_types = array(
	'scheduled'	=> 'Scheduled',
	'unscheduled'	=> 'Unscheduled',
	'progressive'	=> 'Progressive',
	'nc'		=> 'No Crew',
);
?>
<select tabindex="1" name="dwntime_type" size="1" width="107">
	<?php foreach($dwntime_types as $value => $name) { ?>
		<option value="<?php echo $value ?>"<?php if($value == $dwntime_type) { ?> selected="selected"<?php } ?>><?php echo $name ?></option>
	<?php } ?>
</select>

As you can see, it completly cuts out your IF statement, and the only thing you need to maintain is the array at the top.
Jack




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users