Welcome Guest, Not a member yet? Register   Sign In
Problem with checkboxes
#1

[eluser]Johan André[/eluser]
I searched the forums alot for a solution to the problem that checkboxes don't get set correctly when using
Code:
set_checkbox()

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>
&lt;input type="checkbox" name="my_checkbox" &lt;?php echo checkbox_check('my_checkbox', $item['my_checkbox']); ?&gt; value="1" /&gt;
&lt;?php echo form_error('my_checkbox'); ?&gt;
</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...


Messages In This Thread
Problem with checkboxes - by El Forum - 02-15-2009, 05:14 PM
Problem with checkboxes - by El Forum - 02-16-2009, 03:44 AM
Problem with checkboxes - by El Forum - 02-16-2009, 04:55 AM
Problem with checkboxes - by El Forum - 02-16-2009, 05:05 AM
Problem with checkboxes - by El Forum - 05-03-2009, 12:26 PM
Problem with checkboxes - by El Forum - 05-04-2009, 11:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB