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

[eluser]vertmonkee[/eluser]
In my form I have some checkboxes that are created based on a database table.

The table contains organisations the user can agree to share data with.

The organisations are put into an array with their id as the key and name as the value
Code:
$organisations = array();

$organisations[21] = 'First organisation';
$organisations[57] = 'Second organisation';
$organisations[98] = 'Third organisation';

I need to create the checkboxes from this which I am happy with. The problem I am having is when the user submits the form with incorrect information. They are then shown the form again but I can't figure out how to 'check' the checkboxes again.

I came up with this which creates the checkboxes but does not recheck them
Code:
<?php
foreach($organisations as $key => $value) {
?>
  <li>
    <label for="organisation">&lt;?php echo $value; ?&gt;</label>
    &lt;?php echo form_checkbox("organisation[]", array(
                    'name'        => 'organisation[]',
                    'value'        => $key,
                    'checked'     => set_checkbox('organisation[]', $key)                
    )); ?&gt;    
  </li>
&lt;?php
}
?&gt;

Any help would be greatly appreciated.

Thanks
#2

[eluser]theshiftexchange[/eluser]
Are you using form_validation? If so - you MUST set a rule for each checkbox - even if the rule itself is blank. This way CI knows you expect the checkbox to be re-populated during a failed form_validation
#3

[eluser]ToddyBoy[/eluser]
I was having this problem as well, trying to set a check box/radio button to be checked if a value is matching the value in an array from my database.

I came up with this solution.

HTML for the check box
Code:
&lt;input type="checkbox" name="appliance_name[]" value="Stove"
&lt;?php $appliance = 'Stove'; echo set_checkbox('appliance_name[]',
array_check($appliance,$appliance_array)); ?&gt; /&gt;

Inside the checkbox is the set_checkbox function for form validation. I also created a variable called $appliance to handle the value of the check box. I then call a function called array_check($appliance,$appliance_array) to see if the value of the array from my database, $appliance_array, matched the value of the $appliance variable.

My function
Code:
function array_check($appliance,$appliance_array){
    for ($index = 0; $index < count($appliance_array); $index++){
        if ($appliance_array[$index] == $appliance){ echo("CHECKED");}
    }
}

Feel free to break this apart and suggest a better method. It's the first solution I have created myself where I could not find the answers on this forum or Google.
#4

[eluser]RickP[/eluser]
set_checkbox('opt1','one') will return 'checked="checked"' IFF the value of opt1 is 'one' otherwise it will return an empty string.

So if you pass an array of parameters to form_checkbox() you can use:

Code:
$data = array('name'=>'opt1','value'=>'one','checked'=>strlen(set_checkbox('opt1','one'))>0);
echo form_checkbox($data);

When using an array argument to form_checkbox() the value of 'checked' should be TRUE or FALSE and this is what strlen() > 0 returns.




Theme © iAndrew 2016 - Forum software by © MyBB