选择选项仅传递0

时间:2017-09-08 01:59:32

标签: php twig silex

以下是我的表格中的树枝

<div class="input-group col-lg-11 col-lg-offset-1">
    <span>Type:</span>
    <select name="group-year-type[]" class="form-control year-type">
        <option value="0">Drama</option>
        <option value="1">Non-Drama</option>
    </select>
    <span>Year:</span>
    <input required="require" type="number" min="1950" max="2050" size="16" maxlength="10" class="form-control" name="group-year">
    <span>Status:</span>
    <select name="group-year-status[]" class="form-control year-status">
        <option value="0">Hidden</option>
        <option value="1">Show</option>
    </select>
</div>

他们传递给我的控制器,如下所示

            $formData = $request->request->all();
            $type = $formData['group-year-type'];
            $year = $formData['group-year-status'];
            $status = $formData['group-year-status'];
            $detect = '';
            $this->app['query']->insertDrama($type,$year,$status);

虽然我的模块会将它们放入db,如下所示

function insertDrama($type,$year,$status){
    $sql = array(
        'year_type' => $type,
        'year' => $year,
        'year_status'=>$status,
    );
    return $this->app['db']->insert('drama_cat', $sql);
}

我不知道为什么,year已正确插入,但statustype的值始终为0。请帮忙。

1 个答案:

答案 0 :(得分:1)

从您的选择框中删除您的姓名属性中的[]。将它们设为name="group-year-status"等等。

问题是它创建了如下数组: -

Array(
    'group-year-status'=>Array(
           0 => 'your selected value'
     )
);

这就是为什么在获取它时的某些方法总是得到0

注意: - 我不知道Silex框架以及它如何解释$formData,但问题的根本原因仅限于此。