Welcome Guest, Not a member yet? Register   Sign In
set_checkbox() ... Array documentation?
#1

[eluser]MTWIGG[/eluser]
I am trying to create a form which uses checkboxes, but the documentation on the set_checkbox function is unclear. It states...

Quote:set_checkbox()
Permits you to display a checkbox in the state it was submitted. The first parameter must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:
<input type="checkbox" name="mycheck[]" value="1" <?php echo set_checkbox('mycheck[]', '1'); ?> />
<input type="checkbox" name="mycheck[]" value="2" <?php echo set_checkbox('mycheck[]', '2'); ?> />

but it does not state how the array variable should be filled.

I have a controller with the lines...

Code:
$d_checkboxes = Array('option1', 'option3');
$body_data['d_checkboxes'] = $d_checkboxes;
$this->load->view('form_body', $body_data);

and a body with the following lines ...

Code:
<input type="checkbox" name="d_checkboxes[]" value="option1" <?php echo set_checkbox('d_checkboxes[]','option1'); ?>
<input type="checkbox" name="d_checkboxes[]" value="option2" <?php echo set_checkbox('d_checkboxes[]','option2'); ?>
<input type="checkbox" name="d_checkboxes[]" value="option3" <?php echo set_checkbox('d_checkboxes[]','option3'); ?>

I would expect option1 and opion3 to be checked, but they are not. What should I be passing? (I have looked at the ... Using Arrays as Field Names ... part of the Form Validation document but this does not help).

I also have included in the controller the line

Code:
$this->form_validation->set_rules('d_checkboxes[]', 'Checkboxes', '');

so if other elements of the form are incorrect then the checkboxes will redisplay in the state they were when submit is pressed ... but this also does not work, which may be related to the above problem.




Theme © iAndrew 2016 - Forum software by © MyBB