deathstrike369 Posted June 15, 2011 Share Posted June 15, 2011 I need to know if there is anyway i can use either sessions or the form submission it self to make a combo box have the selected value from the form showing after the page reloads (its submitted to itself) if there is a way i would like to know how to do it, or if there is a site i can read up on it. <select name="defpt" value="<?php echo htmlentities($_post["defpt"]); ?>"> <option value=".9">attacker</option> <option value=".9">Economist</option> <option value="1.15">Miner</option> <option value="1.5">Explorer</option> <option value="1">Standard</option> <option value="1.1">Researcher</option> </select> <br /> Council: <select name="DefensiveCouncil"> <option value="1.2">System Commander</option> <option value="1.15">Vice System Commander</option> <option value="1.15">Fleet Admiral</option> <option value="1.12">Vice Fleet Admiral</option> <option value="1">Starship Fleet Leader</option> <option value="1.12">Fighter squadron leader</option> <option value="1">Intelligence Officer</option> <option value="1">Secretary of treasury</option> <option value="1">Nooby Advisor</option> this are part of the combo boxes i am using, as you see i tried to use the session to set a default but it did not work. Quote Link to comment https://forums.phpfreaks.com/topic/239410-combo-box-help/ Share on other sites More sharing options...
Novocaine88 Posted June 15, 2011 Share Posted June 15, 2011 Firstly you would want to get your post value if the form has been submitted. if($_POST) { $defpt = $_REQUEST['defpt']; } Then use a function like so; function SelectIt($Value1, $Value2) { global $Select; if($Value1 == $Value2) { $ReturnValue = "selected=\"selected\""; $Select = "Selected"; } else { $ReturnValue = ""; } return($ReturnValue); } you would then construct your select tag like so <select name="defpt"> <option value=".9" <?php echo SelectIt(.9 , $defpt) ?>>attacker</option> <option value=".9" <?php echo SelectIt(.9 , $defpt) ?>>Economist</option> // this line would need to have a different value to the one above <option value="1.15" <?php echo SelectIt(1.15 , $defpt) ?>>Miner</option> <option value="1.5" <?php echo SelectIt(1.5 , $defpt) ?>>Explorer</option> <option value="1" <?php echo SelectIt(1 , $defpt) ?>>Standard</option> <option value="1.1" <?php echo SelectIt(1.1 , $defpt) ?>>Researcher</option> </select> having the same value for more than one option will cause this to not work properly. If you need some options with the same value it might be better to make the values of the option unique, but handle what those unique values equal post form. e.g. if($defpt == "attacker") { $value = 0.9; } if($defpt == "Economist") { $value = 0.9; } if($defpt == "Miner") { $value = 1.15; } // etc... Quote Link to comment https://forums.phpfreaks.com/topic/239410-combo-box-help/#findComment-1229928 Share on other sites More sharing options...
Fadion Posted June 15, 2011 Share Posted June 15, 2011 This question has been asked a dozen times recently. Anyway, you have 2 options to retain the selected value. <select name="defpt"> <option value=".9" <?php if ($_POST['defpt'] == '.9') { echo 'selected="selected"'; } ?>>attacker</option> <option value=".9" <?php if ($_POST['defpt'] == '.9') { echo 'selected="selected"'; } ?>>Economist</option> <option value="1.15" <?php if ($_POST['defpt'] == '1.15') { echo 'selected="selected"'; } ?>>Miner</option> <option value="1.5" <?php if ($_POST['defpt'] == '1.5') { echo 'selected="selected"'; } ?>>Explorer</option> <option value="1" <?php if ($_POST['defpt'] == '1') { echo 'selected="selected"'; } ?>>Standard</option> <option value="1.1" <?php if ($_POST['defpt'] == '1.1') { echo 'selected="selected"'; } ?>>Researcher</option> </select> What it does is simply check if the POST value of 'defpt' is equal to an option and if yes, put a "selected" attribute. Beware that you have two options with the same value. The second way would be much easier to create and handle by using PHP arrays. <select name="defpt"> <?php $options = array(0.9=>Attacker, 1.15=>Miner, 1.5=>Explorer, 1=>Standart, 1.1=>Researcher); foreach ($options as $value=>$name) { $selected = ''; if ($value == $_POST['defpt']) { $selected = 'selected="selected"'; } echo '<option value="' . $value . '" ' . $selected . '>' . $name . '</option>'; } ?> </select> What I did was put all the options in an array ("value" as key, name as value) and printed all them in a loop. Used the $selected variable to check if the option was selected or not. Quote Link to comment https://forums.phpfreaks.com/topic/239410-combo-box-help/#findComment-1229933 Share on other sites More sharing options...
deathstrike369 Posted June 15, 2011 Author Share Posted June 15, 2011 thanks for the replies, i tried to look for the answer i figured someone would of asked i did not find any relevant ones, all the ones asked were dealing with mySQL which i am not utilizing ill try them out and see which one works best for my use thanks again Quote Link to comment https://forums.phpfreaks.com/topic/239410-combo-box-help/#findComment-1229939 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.