Welcome Guest, Not a member yet? Register   Sign In
set_checkbox isn't working - how can I troubleshoot it?
#1

[eluser]Samuurai[/eluser]
Hi,

I've spent an entire day trying to get this working and am hoping someone can shed a little light.

I have a form which I use the form_validation plugin with. The validation rules are working perfectly.

I use set_value and set_checkbox to repopulate the form. set_value is working fine, but none of my checkboxes repopulate.

Here is an example from my form:
Code:
<input type="checkbox" class="checkbox" id="hgv1" name="skills[]" value="hgv1" <?=set_checkbox('skills[]',@$result['hgv1'])?> />
<input type="checkbox" class="checkbox" id="hgv2" name="skills[]" value="hgv2" <?=set_checkbox('skills[]',@$result['hgv2'])?> />

here is the relevant parts of my controller:
Code:
function index()
    {
        if($this->input->post('submit'))
        {
            $this->_do_save();
        }
        else
        {
                $this->_populate_user_form();
        }
        $this->load->view('header', $data);
        $this->load->view('user_form', $data);
        $this->load->view('footer', $data);
    }

    function _do_save()
    {
        //SET VALIDATION RULES
        $this->form_validation->set_rules('skills[]','Skills','callback_skill_check|xssclean');
        
        if($this->form_validation->run())
        {
            //SAVE USER
        }
    }
    function _populate_user_form()
    {
        // GET DATA FROM DB and pass to form
    }
#2

[eluser]Samuurai[/eluser]
I realised that instead of this:
Code:
<input type="checkbox" class="checkbox" id="hgv1" name="skills[]" value="hgv1" <?=set_checkbox('skills[]',@$result['hgv1'])?> />

it should be this:
Code:
<input type="checkbox" class="checkbox" id="hgv1" name="skills[]" value="hgv1" <?=set_checkbox('skills[]','hgv1])?> />

The only thing is, how can I re-use my form in edit mode?
This should work:
Code:
<input type="checkbox" class="checkbox" id="hgv1" name="skills[]" value="hgv1" <?=if(isset($result['skills']['hgv1']) ? echo " checked=checked" : set_checkbox('skills[]',@$result['hgv1'])))?> />
But it feels like far too much PHP in my view.. is there a nicer way?




Theme © iAndrew 2016 - Forum software by © MyBB