dink87522 Posted July 24, 2010 Share Posted July 24, 2010 <form name="statCompany" method="post" action="statCompanies.php"> <label>Sort by:</label> <select name='criteria'> <option value='nameAsc'>Name (Asc)</option> <option value='nameDesc'>Name (Desc)</option> <option value='ceoAsc'>CEO (Asc)</option> <option value='ceoDesc'>CEO (Desc</option> <option value='moneyAsc'>Money (Asc)</option> <option value='moneyDesc'>Money (Desc)</option> </select> <label><button type="submit" name="sort" id="sort" value="Sort"> Sort </button> </form> How can I dynamically set one of these in the drop down list as the default? I.e. the top one is fine originally, but then after the user has selected the third one say and the page reloads, I want the third one to be the default selected then (suing PHP if possible). Quote Link to comment https://forums.phpfreaks.com/topic/208736-php-listbox-default/ Share on other sites More sharing options...
msaz87 Posted July 24, 2010 Share Posted July 24, 2010 Something like this: <?php $critera = $_REQUEST['criteria']; ?> <form name="statCompany" method="post" action="statCompanies.php"> <label>Sort by:</label> <select name='criteria'> <option <?php if($criteria == "nameAsc") echo "selected"; ?> value='nameAsc'>Name (Asc)</option> <option value='nameDesc'>Name (Desc)</option> <option value='ceoAsc'>CEO (Asc)</option> <option value='ceoDesc'>CEO (Desc</option> <option value='moneyAsc'>Money (Asc)</option> <option value='moneyDesc'>Money (Desc)</option> </select> <label><button type="submit" name="sort" id="sort" value="Sort"> Sort </button> </form> Look at the first one as an example and then add this, with the proper value for that option, it its place: <?php if($criteria == "value") echo "selected"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/208736-php-listbox-default/#findComment-1090517 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.