ibanez270dx Posted August 16, 2006 Share Posted August 16, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/17747-autoselect-drop-down-values/ Share on other sites More sharing options...
Jak Posted August 16, 2006 Share Posted August 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/17747-autoselect-drop-down-values/#findComment-75709 Share on other sites More sharing options...
ibanez270dx Posted August 16, 2006 Author Share Posted August 16, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/17747-autoselect-drop-down-values/#findComment-75725 Share on other sites More sharing options...
radalin Posted August 16, 2006 Share Posted August 16, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/17747-autoselect-drop-down-values/#findComment-75731 Share on other sites More sharing options...
Jak Posted August 17, 2006 Share Posted August 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/17747-autoselect-drop-down-values/#findComment-76117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.