Reputation: 37
I have generated a form, a number of the fields are multiple selects. The values are generated from a foreach (due to the required format):
echo '<select name="data[formdata]['.$question['PrPageQuestion']['ID'].']" id="formdata'.$question['PrPageQuestion']['ID'].'" multiple="multiple">';
foreach($contacts as $contact):
echo '<option value="'.$contact['PrDataContact']['ID'].'">'.$contact['PrDataContact']['Name'].' ('.$contact['PrDataContact']['Email'].')</option>';
endforeach;
echo '</select>';
However when i select multiple and submit the form, i run the debug only one value is being passed, not a string as expected.
The html output is:
<select name="data[formdata][2]" id="formdata2" multiple="multiple">
<option value="13">A Contact ([email protected])</option>
<option value="14">A. Nother-Contact ([email protected])</option>
<option value="15">A. New. Contact ([email protected])</option>
<option value="17">New Conti ([email protected])</option>
</select>
The output of var_dump is:
array(2) {
["_method"]=> string(4)
"POST" ["data"]=> array(1) {
["formdata"]=> array(8) {
//other data was here
[1]=> string(1) "1"
[2]=> string(2) "15"
[3]=> string(1) "4"
[4]=> string(0) ""
}
}
}
Upvotes: 2
Views: 1148
Reputation: 2693
You need to add [ ] after the name to tell html that this is an array of values to be submitted
echo '<select name="data[formdata]['.$question['PrPageQuestion']['ID'].'][]" id="formdata'.$question['PrPageQuestion']['ID'].'" multiple="multiple">';
foreach($contacts as $contact):
echo '<option value="'.$contact['PrDataContact']['ID'].'">'.$contact['PrDataContact'['Name'].' ('.$contact['PrDataContact']['Email'].')</option>';
endforeach;
echo '</select>';
You should then get:
array(2) {
["_method"]=> string(4)
"POST" ["data"]=> array(1) {
["formdata"]=> array(8) {
//other data was here
[1]=> string(1) "1"
[2]=> array(2) {
[0]=>string(2) "15"
[1]=>string(2) "17"
},
[3]=> string(1) "4"
[4]=> string(0) ""
}
}
}
Upvotes: 2