Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Pre-selecting options in multiple select box on multi-page form

Multiple select boxes

4:33 pm on Dec 15, 2012 (gmt 0)

New User

5+ Year Member

joined:July 10, 2009
posts: 4
votes: 0

I'm working on a multi-page form which the client has now requested they'd like a 'back' button. Not a problem for standard text fields, since I can simply echo the variable carrying the field value into the 'value' attribute, but for multiple select boxes - which can have up to 200 options - I'm not sure writing the following would be terribly efficient:

<option value="Apple" <?php if($vendor=='Apple') (echo 'selected'} ?>>Apple</option>
<option value="Asus" <?php if($vendor=='Asus') (echo 'selected'} ?>>Asus</option>
<option value="Asustek" <?php if($vendor=='Asustek') (echo 'selected'} ?>>Asustek</option>

... that would be a lot of typing! And, because it's a multi-select box, the variable $vendor is potentially going to have multiple values in it - so the above isn't going to work anyway.

I have quite a few of these boxes to deal with, so I'm wondering whether there's a better way of handling them?


4:46 pm on Dec 15, 2012 (gmt 0)

Preferred Member from GB 

10+ Year Member Top Contributors Of The Month

joined:July 25, 2005
posts: 387
votes: 7

Just a quick idea. Not sure because I haven't tested it. I assume you can write the selected values into an array and when the user hits the Back button, the array gets transmitted to the previous location. From there you can restore the selection doing this:

$selectedArray = array(Apple, Asus, Asustek); //you need to generate this dynamically
$options = explode(",", $selectedArray);
foreach($options as $val) {
${"option".$val} = "selected=\"selected\"";

and then build the form field like this:

<option value="Apple" <?php echo $optionApple ?> >Apple</option>
<option value="Asus" <?php echo $optionAsus ?> >Asus</option>
<option value="Asustek" <?php echo $optionAsustek ?> >Asustek</option>

Still a lot of typing though :)
4:52 pm on Dec 15, 2012 (gmt 0)

New User

5+ Year Member

joined:July 10, 2009
posts: 4
votes: 0

Yup - still al lot of typing, but it does give me a thought. I wonder whether the client side could be handed the task of doing this in javascript by iterating through the array and dynamically selecting the options where the values match...

My JS knowledge is a little too limited to know whether it's possible to do that though, but the theory sounds good, eh? ;-)
8:01 pm on Dec 15, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 7, 2003
votes: 0

You need to have somehow the list of items to show and decide which are selected.

Once you have that, all you need to output the html is a single loop.

Is pseudo code:

foreach (iterate over the list of options, store the value in $value) {
if( check if $value is selected ) {
print('<option value="'.$value.'" selected="selected">'.$value.'</option>'."\n");
} else {
print('<option value="'.$value.'">'.$value.'</option>'."\n");

The iteration and check depend on how you store it.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members