Jump to content
Larry Ullman's Book Forums

Retain Multi Select Values


Recommended Posts

I have a multi select combo box on my site. If the user fills in the form wrong the site retains all the information already entered, however the multi select combo box comes back with the selections de-selected.

How can I get the combo box to retain the values entered when the user happens to fill something wrong elsewhere on the form?

<select name="Skills[]" multiple size="1" id="Combobox1">

<option value="LGV Class 1">LGV Class 1</option>
<option value="LGV Class 2">LGV Class 2</option>
<option value="7.5 Ton Drivers">7.5 Ton Drivers</option>
<option value="Non LGV Driver">Non LGV Driver</option>
<option value="Fork Lift Driver">Fork Lift Driver</option>
<option value="Garage Staff">Garage Staff</option>
<option value="Transport Manager">Transport Manager</option>
<option value="Traffic Planner">Traffic Planner</option>
</select>


 

Link to comment
Share on other sites

You need to do an equality check when you output each option, and add 'selected="selected" to the element. The "tricky" part here is that you allow multiple values, so Skills[] (which you could consider naming for easier reference) could contain between zero to N values. You'll therefor need to loop that array, or write a function that helps you with that.

 

Something along the lines of:

$driverSkills = $_POST['Skills'][]; // Use key or index to fetch actual driving skills here.

function isSelected( $array, $value )
{
   foreach ( $array as $option )
   {
      if ( $option == $value ) return true;
   }
   return false;
}

<option value="Traffic Planner" <? if ( isSelected("Traffic Planner") ? 'selected="selected"' : ''; ?>>Traffic Planner</option>

The approach is a little naive, but will work. To optimize it, you should consider moving from String values to integers on the option elements. That way, you could be able to both output and check the content in the same operation:

$drivingSkills = array(
   1 => "LGV Class 1",
   2 => "LGV Class 2"
);

$selectedSkills = $_POST['skills']['driving']; // Named keys are simpler

<select name="skills['driving']" multiple size="1" id="Combobox1">
<?php foreach ( $drivingSkills as $value => $name ) : ?>
   <?php if ( isset($selectedSkills[$value]) : ?>
      <option value="<?= $value ?>" selected="selected"><?= $name ?></option>
   <?php else; ?>
      <option value="<?= $value ?>"><?= $name ?></option>
   <?php endif; ?>
<?php endforeach; ?>
</select>

You'll need some more safe-guarding like creating an empty array if $_POST['skills'] is unavailable, but that's all to it.

  • Upvote 1
Link to comment
Share on other sites

  • 2 weeks later...

I would just use the built-in in_array function to check whether a value has been selected or not.

For example:

$all_skills = some-array-with-all-of-the-possible-skills;
$selected_skills = another-array-with-only-the-selected-skills;

foreach ($all_skills as $skill) {
  echo '<option' . (in_array($skill, $selected_skills) ? ' selected="selected"' : '') . '></option>';
}

Of course, you need the extra markup for the option text to be displayed, and the actual select element itself, but I wanted to keep the code simple for the sake of focusing on the key point.

Link to comment
Share on other sites

 Share

×
×
  • Create New...