geroid Posted May 9, 2009 Share Posted May 9, 2009 I have a drop down list on my page that supplies the user with day, month and year options to select (a select tag). Here's my problem: If the user forgets to fill out some part of the form, I return the form to them with all of the parts filled in that they did do correctly. This prevents the user from having to complete the form from scratch. I must return the drop down lists to them also but if it's possible, I would love for the list to be positioned at whatever the user previously chose. Currently the drop down lists are reset even if the user correctly selected an option. I have the user data in a session variable so I know what they selected. Is there any code to go through the list and make it stop at the item that the user originally chose? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 9, 2009 Share Posted May 9, 2009 Storing the options of the list in an array will make your life a whole lot easier here. You can then check each option in turn to see if that is the value of the selectbox. If it is, then set the selected attribute. For example: <?php $options = array("value1"=>"Text 1","value2"=>"Text 3",=>"value4"=>"Text 4"); echo '<select name="selectbox">'; foreach($options as $value => $text){ //has the form been submitted and, if it has, is this option the one that was chosen? $selected = ((count $_POST > 0) && ($_POST['selectbox'] == $value) ) ? 'selected="selected"' : ''; echo '<option> value="'.$value.'" '.$selected.'>'.$text.'</option>'; } echo '</select>'; ?> Quote Link to comment 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.