Multiselect in PHP

by Vincy. Last modified on August 25th, 2022.

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

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

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

The user should hold Ctrl+Shift keys to select multiple values.

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

Display Multi select box in an Add Page

In this PHP example program, we have a list box control with a 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 a user has already (multiple)selected values and those are stored in a database as a 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),

  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 select box input name as an array with square brackets 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 an 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

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “Multiselect in PHP”

  • Baboloki Chibane says:

    Hi there nice site and thanks. I am struggling with a code where from a single table i want to have a form with a dropdown where when i select an option from the dropdown an input box below it will be populated with data from a field in the same table.

    • Vincy says:

      Welcome Baboloki for the nice words. Send me an email with the code / file and details and I will help you out.

  • Gobi Nagaraj says:

    Hi mam,

    This is Gobi. I’m following your code structure and logic it’s Wonderful and most learning part for me. I’ve learned lot from your tutorial. Really this an amazing work for us.

    Thank you so much for posting this good work and initiative for us.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page