How to get multiple items from <select> with PHP?

I’m building a theme options page for my WordPress theme and I would like to have a functionality to select multiple items from a list.

The “one option” select code I use looks like this: http://pastie.org/684800 and it works perfectly.

Read More

I’m a PHP newbie so I tried to modify the above code to achieve the result I want. Here’s what I came up with: pastie.org/684804. As you can see, I basically added a some html values multiple="yes" hoping it will work 😉

The code displays the select item properly, but seems to only save the last selected one. Could someone please give some advice on how to achieve saving multiple chosen items?

Related posts

Leave a Reply

3 comments

  1. If you change the name of the select element to end with “[]”, PHP will treat it as an array. All of the selected items will be elements in the array. For example:

    <select name="myChoices[]" multiple="multiple"> ... </select>
    
    <?php
        $selectedChoices = $_POST['myChoices']; // selectedChoices is an array
    ?>
    
  2. <select name="mySelection[]" multiple="multiple"> 
      <option></option> 
      <option></option>
      <option></option>
    </select>
    

    This will let you to access the multiple choice you in php