Welcome to WebmasterWorld Guest from 54.147.44.93

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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

Multiple select boxes

     

Xandir

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

5+ Year Member



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?

Thanks,

Pete

adder

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

10+ Year Member Top Contributors Of The Month



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:

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

Still a lot of typing though :)

Xandir

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

5+ Year Member



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

swa66

8:01 pm on Dec 15, 2012 (gmt 0)

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



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month