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...
#2

[eluser]TheFuzzy0ne[/eluser]
Hi there. Any chance of seeing the code in your view? The set_checkbox() function works for me.
#3

[eluser]Johan André[/eluser]
Sure! It's basically the same just using set_checkbox().

Code:
<div class="input_field">
    <label for="startpage">&lt;?=lang('projects_startpage');?&gt;</label>
    &lt;input type="checkbox" name="startpage" class="mediumfield" &lt;?=set_checkbox('startpage', $item['startpage']); ?&gt; value="1" /&gt;
    &lt;?php echo form_error('startpage'); ?&gt;
</div>

Strange that it works for some people!
Ofcourse I'm pretty sure the problem is me overseeing something basic... Smile
#4

[eluser]TheFuzzy0ne[/eluser]
Have you checked the value of $item['startpage']? I trust that you have because it's the obvious thing to, but it's the only thing that could possibly be the problem.

Is there any reason why you can't use this code:
Code:
&lt;input type="checkbox" name="startpage" class="mediumfield" &lt;?=set_checkbox('startpage', '1'); ?&gt; value="1" /&gt;

One more thing, (which I'm sure you've already confirmed), does your server support short tags?
#5

[eluser]Unknown[/eluser]
[quote author="Johan André" date="1234761259"]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.
[/quote]

Hi.
If you have loaded the form_validation library in your application, you have to set validation rules for all the fields on your form, also for that fields that don't require validation.

Code:
rules[] = array (
    'field' => 'field_without_rule',
    'label' => '',
    'rules' => ''
    );
  }

Roberto

--
http://speedtech.it
#6

[eluser]cwt137[/eluser]
Hi Johan André. Download a copy of CI from Subversion. This problem should be fixed.




Theme © iAndrew 2016 - Forum software by © MyBB