[eluser]Johan André[/eluser]
I searched the forums alot for a solution to the problem that checkboxes don't get set correctly when using
I found no solution so I created one myself.
Maybe I'm a complete idiot and not able to read the userguide correctly... Anyway, here it is:
1. Create a file named "MY_form_helper.php" in application/helpers. This will extend (actually overload/append) new functions to the form_helper.php supplied in system/helpers/.
2. Add the following code to the file:
Code:
<?php
function checkbox_check($field, $val)
{
if(isset($_POST[$field]))
{
if($_POST[$field] == 1)
{
return ' checked="checked"';
}
return '';
}
if($val == 1)
{
return ' checked="checked"';
}
return '';
}
3. Use like this in your view (assumes you have data in $item['my_checkbox']. 1 = checked, 0 = not checked):
Code:
<label for="my_checkbox">Check it</label>
<input type="checkbox" name="my_checkbox" <?php echo checkbox_check('my_checkbox', $item['my_checkbox']); ?> value="1" />
<?php echo form_error('my_checkbox'); ?>
</div>
The function will check the $_POST-array is the actual key exist. If it does it will use it. If it does'nt the default data (from database or whatever) will be used.
Hope this helps someone...