mbowling Posted August 9, 2007 Share Posted August 9, 2007 When using select multiple in a form how do I retain the highlighting of the selected highlighted values after the form has been submitted? <select name="cities[]" size="4" multiple="multiple" style="font: 8pt Verdana" id="cities_id"> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/64088-select-multiple-in-a-form/ Share on other sites More sharing options...
frost Posted August 9, 2007 Share Posted August 9, 2007 You could check if the value of your option is in the cities array using in_array. Example from php.net: <?php $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?> So in your case you could check if the option value is in an the array and if so set selected: <option value="New York" $selected> Quote Link to comment https://forums.phpfreaks.com/topic/64088-select-multiple-in-a-form/#findComment-319404 Share on other sites More sharing options...
Psycho Posted August 9, 2007 Share Posted August 9, 2007 Because you say you want to "retain the highlighting" I assume you are wanting to redisplay the form with the previously selected options selected: <?php //$_POST['city_id'] : selected options //List of possible options $optionList = (4=>'Los Angeles',12=>'Chicago',13=>'Orlando',22=>'San Francisco',25=>'Denver',29=>'Detroit'); echo '<select name="city_id" size="4" multiple>'; foreach ($optionList as $city_id => $city_name) { echo '<option value="$city_id"'.((in_array($city_id,$_POST['city_id']))?' selected':'').'>$city_name</option>"; } echo '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64088-select-multiple-in-a-form/#findComment-319425 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.