cakephp选择多个只传递第一个值

时间:2012-09-10 15:30:10

标签: php cakephp select

我已经生成了一个表单,一些字段是多个选择。这些值是从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) "" 
        } 
    }
}

1 个答案:

答案 0 :(得分: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) "" 
        } 
    }
}