karenn1 Posted October 17, 2007 Share Posted October 17, 2007 I have a normal drop down menu: <select name="status" class="db_list_text"> <option value="All" selected="selected">All</option> <option value="True">Active</option> <option value="False">Inactive</option> </select> The idea for this is for the user to select either true or false and then to click submit to find data that matches either one. The coding is in the same page so the form basically just reloads the page and grabbes whatever info is requested. How can I with PHP, make it that the dropdown stays on the selected option when the page is reloaded? Thanks, Karen Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/ Share on other sites More sharing options...
zq29 Posted October 17, 2007 Share Posted October 17, 2007 Example: <select name="status" class="db_list_text"> <option value="All" <?php if(isset($_POST['status']) && $_POST['status'] == "All") echo "selected"; ?>>All</option> <option value="True" <?php if(isset($_POST['status']) && $_POST['status'] == "True") echo "selected"; ?>>Active</option> <option value="False" <?php if(isset($_POST['status']) && $_POST['status'] == "False") echo "selected"; ?>>Inactive</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/#findComment-371636 Share on other sites More sharing options...
karenn1 Posted October 17, 2007 Author Share Posted October 17, 2007 Thanks SemiApocalyptic, I'll try that out. One question though. If True and false where values that are dynamically pulled from a database and then selected, would this work: <option value="True" <?php if(isset($_POST['status']) && $_POST['status'] == &_REQUEST['status']) echo "selected"; ?><? $rs['status'] ?></option> The SQL query I use to sample the database is a bit more extensive. The above is just an example. I'm not too worried about the value at this stage, just to get it to display the dynamically selected option when the page reloads. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/#findComment-371827 Share on other sites More sharing options...
karenn1 Posted October 18, 2007 Author Share Posted October 18, 2007 Can anybody else please help? The code that I actually want to use looks like this: foreach($area_list as $area) { print "<option if \"". if(isset($_POST['areas']) && ($_POST['areas'] == $_REQUEST['areas'])) echo $_REQUEST['areas'] ."\" value=\"".$area."\">".ucwords($area)."</option>"; } I've now implemented SemiApocalyptic's code as well but the page gives me a unexpected T_IF error. Please help! Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/#findComment-372157 Share on other sites More sharing options...
mattal999 Posted October 18, 2007 Share Posted October 18, 2007 <select name="status" class="db_list_text"> <option value="All" <?php if(isset($_POST['status']) && $_POST['status'] == "All") { echo "selected"; } ?>>All</option> <option value="True" <?php if(isset($_POST['status']) && $_POST['status'] == "True") { echo "selected"; } ?>>Active</option> <option value="False" <?php if(isset($_POST['status']) && $_POST['status'] == "False") { echo "selected"; } ?>>Inactive</option> </select> try that Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/#findComment-372164 Share on other sites More sharing options...
karenn1 Posted October 18, 2007 Author Share Posted October 18, 2007 Hi Matal999, thanks for you reply. Please have a look at my post just above yours. I need this actually to work with the following code, and not the code you supplied: foreach($area_list as $area) { print "<option if \"". if(isset($_POST['areas']) && ($_POST['areas'] == $_REQUEST['areas'])) echo $_REQUEST['areas'] ."\" value=\"".$area."\">".ucwords($area)."</option>"; } The above gives me an unexpected T_IF error. Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/#findComment-372174 Share on other sites More sharing options...
karenn1 Posted October 18, 2007 Author Share Posted October 18, 2007 Maybe I should re-phrase my question. I apologize for any inconvenience. I have the following code in the top of my page: $sql_dd = "SELECT DISTINCT (Venue_Area) FROM $db.web_summary_info ORDER BY Venue_Area ASC"; $result_dd = mysql_query($sql_dd); Then, my dropdown looks like this: <select name="areas" onchange="window.document.searchform.submit();"> <option value="" selected="selected">Select One</option> <?php while ($rs_dd = mysql_fetch_array($result_dd)) { print "<option ".(isset($_POST['Venue_Area']) && $_POST['Venue_Area'] == 'Newlands'? 'selected ' : '')."value=\"".$rs_dd["Venue_Area"]."\">".ucwords($rs_dd["Venue_Area"])."</option>"; } ?> </select> Currently, this doesn't work at all. Can anybody help me please?? Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/#findComment-372180 Share on other sites More sharing options...
Barand Posted October 18, 2007 Share Posted October 18, 2007 <?php $sql_dd = "SELECT DISTINCT (Venue_Area) FROM $db.web_summary_info ORDER BY Venue_Area ASC"; $result_dd = mysql_query($sql_dd); ?> <select name="areas" onchange="window.document.searchform.submit();"> <option value="" selected="selected">Select One</option> <?php while ($rs_dd = mysql_fetch_array($result_dd)) { $selected = (isset($_POST['Venue_Area']) && $_POST['Venue_Area'] == $rs_dd["Venue_Area"]) ? 'selected' : ''; print "<option value='{$rs_dd["Venue_Area"]}' $selected>" . ucwords($rs_dd["Venue_Area"]) . "</option>"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/#findComment-372206 Share on other sites More sharing options...
karenn1 Posted October 22, 2007 Author Share Posted October 22, 2007 Barry, you are an absolute star!!! Works great! Thanks!!!! Karen Quote Link to comment https://forums.phpfreaks.com/topic/73659-solved-select-option-stays-selected/#findComment-375342 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.