tags:

views:

227

answers:

2

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.

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?

+4  A: 

If you give the select a name followed by [] in the form,

 name="my_select[]"

you will get an array in the target PHP script that you can parse.

Pekka
+10  A: 

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
?>
Scott Saunders
Could you please edit the code I have here? http://pastie.org/684892 I'm a complete beginner when it comes to PHP so I'm not sure what do you mean. I have `name="<?php echo $value['id']; ?>"` at the moment, which displays an ID I give it in an array. Should I change it to `name="<?php echo $value['id']; ?>[]"`?
Justine
Yes, you've got it: name="<?php echo $value['id']; ?>[]"
Scott Saunders
I've done it, but it doesn't seem to work. The items from the select element are saved in a variable. But when I echo this variable I don't get "Array" printed out. Here's the full code I used if that will help http://pastie.org/685106 (excuse the lack of code intends and please ignore the table based code ;) )
Justine