views:

61

answers:

3

I know how to send data via a multi-select form object and group the data into an array:

<select name="my_data[]" multiple="multiple"/>

Is it possible to have multiple different select boxes with "single" values and push them all into an array? i.e.

<select id="select-1" name="my_data[]"/>
<select id="select-2" name="my_data[]"/>

result would be

[
    0 => {value of select-1},
    1 => {value of select-2}
]

What would be a good way to combine the data from the selects into an array if not possible?

A: 

While you probably can do it I wouldn't recommend it. Just because it isn't that clear from someone else reading the code.

A better approach is simply to combine them serverside. Assuming:

<select id="select-1" name="data_1[]"/>
...

<select id="select-2" name="data_2[]"/>
...

On the PHP side:

$data1 = $_POST['data_1'];
$data2 = $_POST['data_2'];
$combined = array_merge($data1, $data2);
cletus
You might wanna change your php to:$_POST['data_1'];$_POST['data_2'];
Ambrosia
bobince
+1  A: 

This assume you're getting all <select>'s on the page:

// get all selects
var boxes = document.getElementsByTagName("select"),
    arr = []; // your final values array

// for each select, pull out the value and push it into 'arr'
for(var i = 0, len = boxes.length; i < len, i++) {
  arr.push(boxes[i].value);
}
Mark Ursino
+1  A: 

Ah, just removing multiple seems to work. Zend Framework's FormSelect helper automatically added it when you have "[]" in your form element name and I did not realize that.

Typeoneerror
Interesting! And by interesting I mean annoying and stupid. :-)
bobince