Reputation: 5784
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?
Upvotes: 2
Views: 1952
Reputation: 2300
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>
Upvotes: 2
Reputation: 2790
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.
Upvotes: 0