'Codeigniter set_checkbox() keeping checked state
I have this code in my view:
<input type="checkbox" name="test[]" value="1" <?php echo set_checkbox("test[]", "1") ?> >test1
<input type="checkbox" name="test[]" value="2" <?php echo set_checkbox("test[]", "2") ?> >test2
These checkboxes are not required, but some other fields on the form are. I want to keep the state of these checkboxes when I post the form. The values from the checkboxes are in the POST if they are checked, but the checked state is not kept when the form loads again after the post (if some of them is checked before the post, I want it to be checked after the post). I also tried set_checkbox(“test”, “1”), but it didn’t work. The values on the other text fields is kept using the set_value() function and is working fine.
Solution 1:[1]
I realised that set_checkbox takes 3 parameters :
set_checkbox(string $checkboxname, string $value, boolean $isChecked);
For example :
echo form_checkbox('mycbx[]',
$item['id'],
set_checkbox('mycbx[]', $item['id'], false)
);
or this way :
$checkbox = array(
'name' => 'mycbx[]',
'value' => $item['id'],
'checked' => set_checkbox('mycbx[]', $item['id'], false)
);
echo form_checkbox($checkbox);
Solution 2:[2]
In your view add a line like this:
<input type="checkbox" name="mycheck[]" value="1" <?php echo set_checkbox('mycheck[]', '1', true); ?> />
The above code assumes that you want the box checked by default, and if not then just change the 3rd parameter to false.
Then in your validation add a line with no rules(so codeigniter knows it has to do something with it). Here's an example:
$this->load->library('form_validation');
$this->form_validation->set_rules('mycheck[]', 'My message that no one will ever see.', '');
Lastly, make sure that you DON'T explicitly set the input item as checked via the input state. That will override the codeigniter parameter. In otherwords DON'T do this <input checked />.
And that's it. Should work like charm.
Solution 3:[3]
Remove the [] from the set_checkbox method call.
For a more informed answer see Repopulating checkboxes in Codeigniter after Unsuccessful Form Validation
Solution 4:[4]
set_checkbox('mycheckbox', 'value',TRUE)
Solution 5:[5]
When you're using an array like "test[]" in form, you don't need to include the square brackets ([]) in your set_checkbox call.
The set_checkbox should always be like this:
set_checkbox('test', 'value');
Where 'value' is the second parameter of the form checkbox.
Solution 6:[6]
Try this guy, is working for me :)
just put "value" inside []
<input type="checkbox" name="test[1]" value="1" <?php echo set_checkbox("test[1]", "1") ?> >test1
<input type="checkbox" name="test[2]" value="2" <?php echo set_checkbox("test[2]", "2") ?> >test2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Community |
| Solution 4 | Rajesh RK |
| Solution 5 | Nikunj Dhimar |
| Solution 6 | Thiago Aurélio |
