kdigital Posted November 26, 2012 Share Posted November 26, 2012 Hello, the following code works for all of my other selects but not the one for height. I know it has something to do with the quotes and HTML symbols. Does anyone know how to fix this? Thanks. <option <?php if(isset($_POST)){if($_POST == "5'0""){echo "selected";}} ?> value="5'0"">5'0"</option> Quote Link to comment https://forums.phpfreaks.com/topic/271211-retaining-drop-down-select-values-after-submitting-a-form/ Share on other sites More sharing options...
Jessica Posted November 26, 2012 Share Posted November 26, 2012 You'll need to run the posted value through htmlentities before trying to compare it to a value that is already encoded. Quote Link to comment https://forums.phpfreaks.com/topic/271211-retaining-drop-down-select-values-after-submitting-a-form/#findComment-1395304 Share on other sites More sharing options...
AyKay47 Posted November 26, 2012 Share Posted November 26, 2012 Also, you are trying to compare the superglobal array $_POST instead of one of its elements. Quote Link to comment https://forums.phpfreaks.com/topic/271211-retaining-drop-down-select-values-after-submitting-a-form/#findComment-1395310 Share on other sites More sharing options...
Psycho Posted November 26, 2012 Share Posted November 26, 2012 (edited) I would highly suggest creating a function to create and auto-select the options instead of putting the code for each and every option. I typically define my options as an array and then pass the array and the currently select value (if there is one) to a function. Also isset($_POST) is worthless since it will always be set even if there was no form posted. Lastly, I would also consider changing the values of the options to something that won't cause problems with HTML markup. The displayed label can still be with the quote marks though. In my example below, I used what 'appears' to be a decimal value, but is just the feet/inches separated by a period. This is just a rough example <?php $height_options = array( '5.0' => "5'0\""; '5.2' => "5'2\""; '5.4' => "5'4\""; '5.6' => "5'6\""; '5.8' => "5'8\""; '5.10' => "5'10\""; '5.0' => "6'0\""; ); function createOptions($optionsAry, $selectValue=false) { $optionsHTML = ''; foreach($optionsAry as $value => $label) { $value = htmlspecialchars($value); $label = htmlspecialchars($label); $selected = ($selectValue===$value) ? ' selected="selected"' : ''; $optionsHTML .= "<option value='{$value}'>{$label}</option>\n"; } return ; } ?> <select name="height"> <>php echo createOptions($height_options, $_POST['height']); ?> </select> Edited November 26, 2012 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/271211-retaining-drop-down-select-values-after-submitting-a-form/#findComment-1395311 Share on other sites More sharing options...
AyKay47 Posted November 26, 2012 Share Posted November 26, 2012 A nice and concise example above. I cleaned up Psyco's code a bit from the rough example: $height_options = array( '5.0' => "5'0\"", '5.2' => "5'2\"", '5.4' => "5'4\"", '5.6' => "5'6\"", '5.8' => "5'8\"", '5.10' => "5'10\"", '5.0' => "6'0\"", ); function createOptions($optionsAry, $selectValue=false) { $optionsHTML = ''; foreach($optionsAry as $value => $label) { $label = htmlspecialchars($label); $selected = ($selectValue === $value) ? ' selected="selected"' : ''; $optionsHTML .= "<option value='{$value}'{$selected}>{$label}</option>\n"; } return $optionsHTML; } ?> <select name="height"> <?php echo createOptions($height_options, $_POST['height']); ?> </select> The above code will work if you follow psycos' advice and go with the decimal scheme, which will make your life much easier. Quote Link to comment https://forums.phpfreaks.com/topic/271211-retaining-drop-down-select-values-after-submitting-a-form/#findComment-1395337 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.