문제

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?

도움이 되었습니까?

해결책

You need to assign keys to the items you're adding to the $ddmenu; Eg. $ddmenu[$tablerow['idcategorieen']] = $tablerow['idcategorieen']; will make the values be the idcategorieen.

Edit for clarification: If $ddmenu looks like this:

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

The dropdown options will look like

<option value="1">Books</option>
<option value="2">Cats</option>
<option value="3">Foo Bars</option>

다른 팁

Have some solutions to this.

When you call form_dropdown, the codeigniter do your dropdown based on key=>value array.

So key gonna be your option value, and value gonna be your option label.

You can do 2 things, format your "$opties" to the expected format, or take the submit before it happens with javascript, and post the option label instead the value.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top