Welcome Guest, Not a member yet? Register   Sign In
Repopulating checkbox using CI syntax: confused.
#1

[eluser]e-man[/eluser]
I'm working on a form built completely using the CI syntax and I'm looking for a way to repopulate a checkbox using that very syntax.
This is the code for my checkbox:
Code:
echo form_label('Industrie', 'bestemming');
        $data = array(
                      'name'        => 'bestemming[]',
                      'id'          => 'industrie',
                      'value'       => 'industrie',
                      'type'        => 'checkbox'
                    );
        echo form_input($data);

Now how do I combine this with the set_checkbox function?:
Code:
echo set_checkbox('bestemming', 'industrie');
#2

[eluser]moonbeetle[/eluser]
Hi, from what I see in the user guide, set_checkbox() isn't meant to be used with the form_checkbox() method.
Looks like set_checkbox() can only be used if you literally follow the example in the user guide, thus adding checkboxes in plain old HTML style, not CI generated style.

Found a small error in your code, to output a checkbox CI style, use form_checkbox() instead of form_input.
Also, for a checkbox, the data array can have a key called 'checked' which you set to have a value of TRUE/FALSE. This can be done hardcoded or dynamically (by matching the value of the checkbox).

Code:
// CI generated checkbox, checked='checked' by default
$data = array(
    'name'        => 'bestemming[]',
    'id'          => 'industrie',
    'value'       => 'industrie',
    'checked'      => TRUE,
    'type'        => 'checkbox'
);
echo form_checkbox($data);
echo form_label('Industrie', 'industrie');



// CI generated checkbox, checked status determined by value
$checkval = "industrie2"; // quick test
// $checkval should be provided by the controller as part of data passed to the view

$data = array(
    'name'        => 'bestemming[]',
    'id'          => 'industrie2',
    'value'       => 'industrie2',
    'checked'      => (isset($checkval) && $checkval == "industrie2")?TRUE:FALSE,
    'type'        => 'checkbox'
);
echo form_checkbox($data);
echo form_label('Industrie 2', 'industrie2');

About your question, "Repopulating", suggests you want to display the form again, after submit. Worth checking CI's validation class in combination with the form helper and validating arrays.
#3

[eluser]e-man[/eluser]
Cheers mate! Although your second suggestion was even better:
Set up a basic validation rule for the checkbox:
Code:
$this->form_validation->set_rules('bestemming[]', 'Bestemming', 'trim|required');
then in your view you can do:
Code:
$data = array(
                      'name'        => 'bestemming[]',
                      'id'          => 'handelsruimte',
                      'value'       => 'handelsruimte',
                      'checked'        =>  ($this->form_validation->set_checkbox('bestemming[]', 'handelsruimte'))?TRUE:FALSE
                    );
Works really well, thanks!
#4

[eluser]Medikal[/eluser]
I hate to dig up an old thread, but I have a ton of checkboxes such as this:

Code:
&lt;? echo form_checkbox('category[]', 'apparel', ($this->form_validation->set_checkbox('category[]', 'apparel'))?TRUE:FALSE).'Apparel<br>'; ?&gt;

And it is failing to work, any suggestions?




Theme © iAndrew 2016 - Forum software by © MyBB