MikoMak Posted May 31, 2006 Share Posted May 31, 2006 Hi,I have a simple form with validation so if a user has not filled in a required field the form displays again on submit with all other fields filled in.I have day-month-year drop downs and need help with setting the values that the user has already selected when the form loads again after validation so the dropdowns display them and the user doesn't need to select them again.I'm sure I saw something similar on this forum ages ago but can't find it now.Any help would be greatly appreciated.Thanx. Quote Link to comment https://forums.phpfreaks.com/topic/10836-pre-selecting-in-a-drop-down-menu/ Share on other sites More sharing options...
massive Posted May 31, 2006 Share Posted May 31, 2006 Try this:Drop-down for Month:[code]<select name = "month"><option value="<? echo $_POST['month']?>"><? echo $_POST['month']?></option><option>------------------------------------</option> <--try adding this also to seperate the selected item :)<option value ="January">January</option>etc....until December[/code]Drop-down for Day:[code]<select name = "day"><option value="<? echo $_POST['day']?>"><? echo $_POST['day']?></option><option>------------------------------------</option> <--try adding this also to seperate the selected item :)1 to 31same here...[/code]Drop-down for year[code]<select name = "year"><option value="<? echo $_POST['year']?>"><? echo $_POST['year']?></option><option>------------------------------------</option> <--try adding this also to seperate the current selected item :)<option value ="1905">1905</option>[/code]Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/10836-pre-selecting-in-a-drop-down-menu/#findComment-40494 Share on other sites More sharing options...
Yesideez Posted May 31, 2006 Share Posted May 31, 2006 MONTH:[code]<?php $months=array("January","February","March","April","May","June","July","August","September","October","November","December");?><select name="month"><?php for ($i=0;$i<12;$i++) { echo '<option value='.$months[$i].'"'; if ($month==$months[$i]) {echo ' selected';} echo '>'.$months[$i].'</option>'; }?></select>[/code]DAY:[code]<select name="day"><?php for ($i=1;$i<32;$i++) { echo '<option value='.$i.'"'; if ($day==$i) {echo ' selected';} echo '>'.$i.'</option>'; }?></select>[/code]YEAR:[code]<select name="year"><?php for ($i=1900;$i<2007;$i++) { echo '<option value='.$i.'"'; if ($year==$i) {echo ' selected';} echo '>'.$i.'</option>'; }?></select>[/code]Thats the way I'd do it as $month would be the value obtained from the user earlier in the script. Can't guarantee it works as thats off the top of my head but it gives the general idea. The code above also makes all the data for all the drop-down boxes so there's no need to worry about making loads of HTML. Quote Link to comment https://forums.phpfreaks.com/topic/10836-pre-selecting-in-a-drop-down-menu/#findComment-40497 Share on other sites More sharing options...
MikoMak Posted June 1, 2006 Author Share Posted June 1, 2006 Hey guys,thanx for that! I tried Yesideez's array and that worked when I used just int for the month rather than the name. In any case I'm inserting the values in the db in 0000-00-00 format so that's probably best anyway! :)thanx again! Quote Link to comment https://forums.phpfreaks.com/topic/10836-pre-selecting-in-a-drop-down-menu/#findComment-40863 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.