ferpadro Posted September 14, 2007 Share Posted September 14, 2007 Well, the topic name says it everything. I have 3 comboboxes, one for the day, other for the month and another one for the year. Every time i select a value for the three of them and press the "Submit" button, they reset to the default option. Anyway this can be solved? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/69333-solved-make-selected-option-stay-selected-in-a-combo-after-pressing-a-submit-type-butto/ Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 When form comes in you'll obviously see which was selected, then when redraw the form, set appropiate option as selected... Quote Link to comment https://forums.phpfreaks.com/topic/69333-solved-make-selected-option-stay-selected-in-a-combo-after-pressing-a-submit-type-butto/#findComment-348367 Share on other sites More sharing options...
pikemsu28 Posted September 14, 2007 Share Posted September 14, 2007 <select name="selectbox"> <option value="Select">Select</option> <option value="Option 1" <?php if(isset($_POST['selectbox'])){ echo 'selected="selected"';} ?> >Option 1</option> </select> use an if statement to check to see if the $_POST value is valued...if it is then echo 'selected="selected"' to keep the selection when the form reloads. this can work for input fields as well just echo the value of $_POST into the 'value' attribute. hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/69333-solved-make-selected-option-stay-selected-in-a-combo-after-pressing-a-submit-type-butto/#findComment-348369 Share on other sites More sharing options...
chronister Posted September 14, 2007 Share Posted September 14, 2007 I would do something like this.. <?php $year=$_POST['year']; $day=$_POST['day']; $month=$_POST['month']; $months=array( '1'=>'January', '2'=>'February', '3'=>'March', '4'=>'April', '5'=>'May', '6'=>'June', '7'=>'July', '8'=>'August', '9'=>'September', '10'=>'October', '11'=>'November', '12'=>'December' ); ?> <select name="month" size="1"> <option value="" selected="selected">-Month-</option> <?php foreach($months as $k=>$v) { if(isset($month) && $month==$k) { echo '<option value="'.$k.'" selected="selected" >'.$v.'</option>'; } else { echo '<option value="'.$k.'" >'.$v.'</option>'; } } ?> </select> <select name="day" size="1"> <option value="" selected="selected">-Day-</option> <?php $x=1; while($x <= 31) { if(isset($day) && $day==$x) { echo '<option value="'.$x.'" selected="selected">'.$x.'</option>'; } else { echo '<option value="'.$x.'" >'.$x.'</option>'; } $x++; } ?> </select> <select name="select_year"> <option value="" selected="selected">-Year-</option> <?php $y=date('Y',mktime());//get current year $x=$y-100;// determine 100 years ago while($y > $x) // loop through 100 years { if(isset($year)&& $year==$year) { echo '<option value="'.$y.'" selected="selected">'.$y.'</option>'; } else { echo '<option value="'.$y.'" >'.$y.'</option>'; } $y--; } ?> </select> A little long winded I know, but it should do what you want. I like to use an array for most of my date based forms. We know how many months are in a year, so put it in an array, we know that no month is going to have over 31 days, so create a loop for it, and we can determine the year and just display the number of years back we want using a loop. I don' like to hand code all those values in the darn form. Hope this works for ya. p.s. Sorry about the funky formatting in my code. Dunno why it did that. Don't look like that in my script. Nate Quote Link to comment https://forums.phpfreaks.com/topic/69333-solved-make-selected-option-stay-selected-in-a-combo-after-pressing-a-submit-type-butto/#findComment-348391 Share on other sites More sharing options...
ferpadro Posted September 14, 2007 Author Share Posted September 14, 2007 Thanks a lot everyone for your help. I was able to solve it, finally ^^ Quote Link to comment https://forums.phpfreaks.com/topic/69333-solved-make-selected-option-stay-selected-in-a-combo-after-pressing-a-submit-type-butto/#findComment-348579 Share on other sites More sharing options...
enzo24 Posted February 27, 2013 Share Posted February 27, 2013 Thank you Chronister for your reply. Just one note : if(isset($year)&& $year==$year) should be if(isset($year)&& $year==$y) or else all the years are selected & default selected is always last year in the list Thanx a million... am using this code on my site ) Quote Link to comment https://forums.phpfreaks.com/topic/69333-solved-make-selected-option-stay-selected-in-a-combo-after-pressing-a-submit-type-butto/#findComment-1415330 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.