Jump to content

Select tag positioning problem


geroid

Recommended Posts

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?

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.