2

我已经生成了一个表单,其中一些字段是多选的。这些值是从 foreach 生成的(由于所需的格式):

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>';

但是,当我选择多个并提交表单时,我运行调试时只传递了一个值,而不是预期的字符串。

html输出为:

<select name="data[formdata][2]" id="formdata2" multiple="multiple">
    <option value="13">A Contact (a.contact@email.com)</option>
    <option value="14">A. Nother-Contact (a.n.contact@email.com)</option>
    <option value="15">A. New. Contact (anc@email.com)</option>
    <option value="17">New Conti (new@conti.com)</option>
</select>

var_dump 的输出是:

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) "" 
        } 
    }
}
4

1 回答 1

2

需要在名字后面加上 [ ] 告诉 html 这是一个要提交的值的数组

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>';

然后你应该得到:

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) "" 
        } 
    }
}
于 2012-09-10T15:54:15.757 回答