Multiselect in PHP

There are several form controls used to allow user to select input from a group of options. For example, listbox and checkbox.

By default, we can select multiple options from a group of checkboxes. But for listbox control, multiselect option is not provided by default. And, we should set MULTIPLE property with SELECT tag. For setting MULTIPLE property, we can use both of the following ways.

  1. <select name="multiSelect" multiple="multiple">
    ...
    </select>
    
  2. <select name="multiSelect" multiple>
    ...
    </select>
    

User should hold Ctrl+Shift keys to select multiple values.

In this tutorial, we are going to see an example with multiselect option and how to access from PHP. And also, we are going to show how to populate selected values in edit page.

Display Multiselect box in an Add Page

In this PHP example program we have a list box control with set of languages. It will be loaded dynamically from a MySQL table. And the code for loading this multi select box is,

<select name="languages_known[]" multiple="multiple">
<?php
$languages_result = mysql_query("SELECT * FROM languages");
$i=0;
while($languages_stack = mysql_fetch_array($languages_result)) {
?>
<option value="<?=$languages_stack["lang_name"];?>"><?=$languages_stack["lang_name"];?></option>
<?php
$i++;
}
?>
</select>

Populating Multiselect Box using PHP for an Edit Page

Imagine an user has already (multiple)selected values and those are stored in a database as comma separated values. This example is to load them back from that database and populate them into PHP multi select box (as the user already selected),

multiselect_in_php

  1. Explode retrieved languages_known column into an array $users_languages.
  2. Check each value of $languages_stack, if it exists in exploded array $users_languages. We are using PHP in_array() to check.
  3. If match found, we should keep that select option as selected by using SELECTED property.
<select name="languages_known[]" multiple="multiple">
<?php
$users_language = explode(",",$users["languages_known"]);
$languages_result = mysql_query("SELECT * FROM languages");
$i=0;
while($languages_stack = mysql_fetch_array($languages_result)) {
if(in_array($languages_stack["lang_name"],$users_language)) $str_flag = "selected";
else $str_flag="";
?>
<option value="<?=$languages_stack["lang_name"];?>" <?php echo $str_flag; ?>><?=$languages_stack["lang_name"];?></option>
<?php
$i++;
}
?>
</select>

Access Multiselect input Values from PHP

Since, we have specified the selectbox input name as an array with square backets as (languages_known[]), multiple values selected will be submitted as an array.

If we use this array into the UPDATE query on form submit, then it will cause the following PHP error.

Notice: Array to string conversion in...

To avoid this error, we should convert the data type from array into a delimited string and we use implode() for that.

SQL used in Example Code

users.sql

CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(4) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) NOT NULL,
  `languages_known` varchar(255) NOT NULL,
  PRIMARY KEY (`user_id`)
)

languages.sql

CREATE TABLE IF NOT EXISTS `languages` (
  `lang_id` int(4) NOT NULL AUTO_INCREMENT,
  `lang_name` varchar(255) NOT NULL,
  PRIMARY KEY (`lang_id`)
)

Download Multiselect in PHP Source Code

This PHP tutorial was added on October 9, 2013.

«

»