sotusotusotu Posted September 27, 2007 Share Posted September 27, 2007 Hey, I am using the following code to loop 12 months backwards from the current month in a drop down list - using the get method. I'm having difficulties trying to get the option selected once I have submitted the form. For example, I am using this to filter months, so once a month has been selected and submitted, the first option in my drop down list is displayed and not the one that was selected and submitted. So basically, I need to be able to select a month > hit submit and when the form returns, have the drop down list show me the selected item. Hope that made sense. <? echo '<select name="month">'; for($x=0;$x<12;$x++){ $time = strtotime("$x months ago"); $month = date('F Y',$time); $link = strtolower(date('M',$time)); echo '<option value="'.$link.'">'.$month.'</month>'; } echo '</select>'; ?> Does anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/ Share on other sites More sharing options...
localhost1 Posted September 27, 2007 Share Posted September 27, 2007 first u query the selected month from database and display the fetched value greater by one fro the datbase. i think this will help u out Quote Link to comment https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/#findComment-356258 Share on other sites More sharing options...
sotusotusotu Posted September 27, 2007 Author Share Posted September 27, 2007 I am not selecting the month from the database at this point in time. At the top of my page I am using a simple case switch that simply assigns the selected_month variable a value: <? switch ($_GET["month"]){ case "jan": $selected_month = "January"; break; case "feb": $selected_month = "February"; break; <!------------------------etc ----------------------------> Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/#findComment-356260 Share on other sites More sharing options...
hemlata Posted September 27, 2007 Share Posted September 27, 2007 Hello, I tried successfully, the following slightly modified code to allow the submitted value to be selected on selectbox. Check if this could solve your issue. <form method="get" action="select_prob.php"> <?php echo '<select name="month">'; for($x=0;$x<12;$x++){ $time = strtotime("$x months ago"); $month = date('F Y',$time); $link = strtolower(date('M',$time)); if(isset($_GET['month']) && $link == $_GET['month']) { echo '<option value="'.$link.'" selected="selected">'.$month.'</month>'; }else{ echo '<option value="'.$link.'">'.$month.'</month>'; } } echo '</select>'; ?> <input type="submit" value="Check"> </form> Regards, Quote Link to comment https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/#findComment-356270 Share on other sites More sharing options...
sotusotusotu Posted September 27, 2007 Author Share Posted September 27, 2007 Thanks hemlata, It is exactly how I wanted it. Quote Link to comment https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/#findComment-356271 Share on other sites More sharing options...
sotusotusotu Posted September 27, 2007 Author Share Posted September 27, 2007 Sorry just one more thing. I can't seem to add: <option>value="?month=">All months</option> at the top of the drop down. When I add it above the loop it displays nothing. Can this be done? <? echo 'Filter by month:'; echo '<select name="month" style="margin:0 0 0 4px;" class="medium">'; // loop months for($x=0;$x<12;$x++){ $time = strtotime("$x months ago"); $month = date('F Y',$time); $link = strtolower(date('M',$time)); if(isset($_GET['month']) && $link == $_GET['month']) { echo '<option value="'.$link.'" selected="selected">'.$month.'</month>'; } else { echo '<option value="'.$link.'">'.$month.'</month>'; } } echo '</select>'; echo '<input type="submit" value="Filter" class="submit" />'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/#findComment-356275 Share on other sites More sharing options...
hemlata Posted September 27, 2007 Share Posted September 27, 2007 Hello, One of the following way to include option for 'All months' is ... <?php echo 'Filter by month:'; echo '<select name="month" style="margin:0 0 0 4px;" class="medium">'; echo '<option value="?month=">All months</option>'; // loop months for($x=0;$x<12;$x++){ $time = strtotime("$x months ago"); $month = date('F Y',$time); $link = strtolower(date('M',$time)); if(isset($_GET['month']) && $link == $_GET['month']) { echo '<option value="'.$link.'" selected="selected">'.$month.'</month>'; } else { echo '<option value="'.$link.'">'.$month.'</month>'; } } echo '</select>'; echo '<input type="submit" value="Filter" class="submit" />'; ?> Hope, this might solve your issue. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/#findComment-356281 Share on other sites More sharing options...
sotusotusotu Posted September 27, 2007 Author Share Posted September 27, 2007 It does! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/70866-solved-drop-down-menu-problems/#findComment-356294 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.