CodeIgniter Forums

Full Version: Checkboxes problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]variouspixels[/eluser]
I had a fully working form till I tried adding some checkboxes. The checkbox field is required and does display errors if unchecked but previously checked checkboxes are not saved when I get an error anywhere else on the form.

I have played around with a few codeigniter functions (form_checkbox, set_value(options[])) but I am not having any success.

Code:
//View:
<strong class="field">
    <div>
        <label for="interest">Interest <em>(Check all that apply)</em></label>
    </div>
    &lt;input type="checkbox" name="options[]" value="red" /&gt;
    &lt;input type="checkbox" name="options[]" value="blue" /&gt;
    &lt;input type="checkbox" name="options[]" value="green" /&gt;
</strong>
&lt;?php echo form_error('options[]'); ?&gt;



Code:
// Controller
    $this->form_validation->set_rules('options[]', 'Checkboxes', 'required'); // working

many thanks



gemmes

El Forum

[eluser]Ivar89[/eluser]
I am not sure but I thought you also had:
Code:
set_checkbox()
not sure though

El Forum

[eluser]Ivar89[/eluser]
You can also save the vars in your session data before validation.

El Forum

[eluser]variouspixels[/eluser]
grr... I swear thats the first thing I tried.

Anyway, many thanks.

El Forum

[eluser]Ivar89[/eluser]
Ohw lol, I'm srry if you google it you will get to see forums threats similar to this which might give you a better look because to be honoust I am a beginner to with CI soTongue

El Forum

[eluser]variouspixels[/eluser]
It does not matter if your a beginner, set_checkbox worked fine. Thanks.

I did google and search this forum but I think other people had more complicated script problems. I probably had a typo and assumed that it was some misunderstanding of Codeigniter.

El Forum

[eluser]Ivar89[/eluser]
ok^^ glad to helpWink

El Forum

[eluser]helloworldly[/eluser]
Hello,

I am having the same issue. What was your solution exactly?

I'm using CI's form validation. I'm having an issue of getting set_checkbox() working when using multiple checkboxes as an array.

Code:
// more code

&lt;input type="checkbox" value="ABC" name="group[]" &lt;?php echo $this-&gt;validation->set_checkbox('group[]', 'ABC'); ?&gt; />
<label>ABC</label><br/>

&lt;input type="checkbox" value="XYZ" name="group[]" &lt;?php echo $this-&gt;validation->set_checkbox('group[]', 'XYZ'); ?&gt; />
<label>XYZ</label><br/>

// more code

And
Code:
&lt;input type="checkbox" value="XYZ" name="group[]" &lt;?php echo $this-&gt;validation->set_checkbox('group', 'XYZ'); ?&gt; />
isn't working for me either.

Thanks for any help.

El Forum

[eluser]variouspixels[/eluser]
Here is some of my code, seems similar to yours.

view:
Code:
<strong class="field">
    <div>
        <label for="interest">Interest <em>(Check all that apply)</em></label>
    </div>
    &lt;input class="cb" type="checkbox" name="interests[]" value="General" &lt;?php echo set_checkbox('interests[]', 'general'); ?&gt; /&gt;General
    &lt;input class="cb" type="checkbox" name="interests[]" value="Promotion" &lt;?php echo set_checkbox('interests[]', 'promotion'); ?&gt;  /&gt;Promotion
    &lt;input class="cb" type="checkbox" name="interests[]" value="Other" &lt;?php echo set_checkbox('interests[]', 'other'); ?&gt;  /&gt;Other
    &lt;input class="cb" type="checkbox" name="interests[]" value="Stuff" &lt;?php echo set_checkbox('interests[]', 'stuff'); ?&gt;  /&gt;Stuff
</strong>
&lt;?php echo form_error('interest[]'); ?&gt;
controller:
Code:
$this->form_validation->set_rules('interests[]', 'Checkboxes', 'required'); // Interest Checkbox Required working