2

I have a form with a dropdown for categories.

it looks like this:

<tr>
<td><?= form_label('Categorieen'); ?></td>
<td><?= form_dropdown('categorieen', $opties); ?></td>
</tr>

for the $opties i use this code:

    $dbres = $this->db->get('categorieen');
    $ddmenu = array();
    foreach ($dbres->result_array() as $tablerow) {
      $ddmenu[] = $tablerow['idcategorieen'];
    }
    $data['opties'] = $ddmenu;

But when i use this:

$this->input->post('categorieen');

it stores the value of the selected dropdown as an int.

so like this

select:
option1 (gives value 1, because of first option)
option2 (gives value 2, because it's the second option in de dropdown)
etc

How do i save the categoryid to the database instead of the number of the selected value?

4

2 回答 2

2

您需要为要添加到 $ddmenu 的项目分配键;例如。$ddmenu[$tablerow['idcategorieen']] = $tablerow['idcategorieen'];将使值成为 idcategorieen。

编辑澄清:如果 $ddmenu 看起来像这样:

array(
    1 => 'Books',
    2 => 'Cats',
    3 => 'Foo Bars'
)

下拉选项看起来像

<option value="1">Books</option>
<option value="2">Cats</option>
<option value="3">Foo Bars</option>
于 2013-04-26T13:38:20.493 回答
0

对此有一些解决方案。

当您调用 form_dropdown 时,codeigniter 会根据 key=>value 数组进行下拉。

所以 key 将成为您的选项值,而 value 将成为您的选项标签。

您可以做两件事,将您的“$ opties”格式化为预期的格式,或者在使用javascript之前提交,然后发布选项标签而不是值。

于 2013-04-26T13:38:09.537 回答