I have 3 checkbox in my application.And corresponding to each checkbox there is an textbox.When 开发者_开发问答one checkbox is selelecte, I want to enter the value for the correponding textbox.I used the form validation utility to handle this. Its working fine. But my question is when I submit my form with the checkbox as checked, its become unchecked after submission.
If you want to redisplay the same form with values, you are maybe already using echo set_value('field_name')
in each input field's value attribute:
<label for="field">Field</label>
<input id="field" name="field" value="<?php echo set_value('field'); ?>" class="text" type="text">
With checkboxes you do it using the set_checkbox
function this way:
<ul>
<li>
<input id="consult-option1" name="consult[]" value="option 1" <?php echo set_checkbox('consult[]', 'option 1') ?> class="checkbox" type="checkbox">
<label for="consult-option1">Option 1</label>
</li>
<li>
<input id="consult-option2" name="consult[]" value="option 2" <?php echo set_checkbox('consult[]', 'option 2') ?> class="checkbox" type="checkbox">
<label for="consult-option2">Option 2</label>
</li>
<li>
<input id="consult-option3" name="consult[]" value="option 3" <?php echo set_checkbox('consult[]', 'option 3') ?> class="checkbox" type="checkbox">
<label for="consult-option3">Option 3</label>
</li>
</ul>
Notice the echo set_checkbox('consult[]', 'option 1')
inside the input. It echoes the attribute checked="checked"
if it should be checked. The function is inside the Form helper and it receives two parameters, the name of the checkbox and the checkbox's value as defined in it's value attribute.
Please let me know if it's clear or should I edit and write more clearly. Bye
After seeking answer for hours without any success. I came up with this. view:
<br/>
<input type="checkbox" id="active" <?=$is_active;?> name="active">
<br/>controller:<br/>
$this->form_validation->set_rules('active', lang('active'), 'required');
<br/>if ($this->input->post('active') == 'on'){ // active input field is checked.
<br/>$data['is_active'] = 'checked';
<br/>}else{ // active input field is unchecked.
<br/>$data['is_active'] = '';
<br/>}
<br/>
In my case I had to check the rules only when another field was selected so I also used this (But this is just optional)
if ($this->input->post('item_type') == '1') {
$this->form_validation->set_rules('auction_time', lang('Auction time'), 'trim|required');
$this->form_validation->set_rules('active', lang('active'), 'required');
}
精彩评论