homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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

5+ Year Member

Msg#: 4528447 posted 4:33 pm on Dec 15, 2012 (gmt 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?





5+ Year Member

Msg#: 4528447 posted 4:46 pm on Dec 15, 2012 (gmt 0)

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 :)


5+ Year Member

Msg#: 4528447 posted 4:52 pm on Dec 15, 2012 (gmt 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? ;-)


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

Msg#: 4528447 posted 8:01 pm on Dec 15, 2012 (gmt 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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved