Jump to content

Autoselect Drop Down Values


ibanez270dx

Recommended Posts

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
Link to comment
Share on other sites

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:

[code]<option value="1" selected="selected">Test</option>[/code]

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.
Link to comment
Share on other sites

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=php:0]
if($dwntime_type == "unscheduled")
{
$unscheduled_s = "selected";
}
if($dwntime_type == "progressive")
{
$progressive_s = "selected";
}
if($dwntime_type == "No Crew")
{
$nc_s = "selected";
}
[/code]


HTML:
[code]
<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>
[/code]

Anyone know what I'm doing wrong or what I can do to fix this?

Thanks,
- Jeff
Link to comment
Share on other sites

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:
[code]
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][/code]
Link to comment
Share on other sites

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

[code]<?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>[/code]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.