Welcome Guest, Not a member yet? Register   Sign In
Can't get my checkbox pre-populated
#1

[eluser]louisl[/eluser]
The MyInput field is initially populating as expected but the MyCheckbox isn't checked? Have I got this right?

Everything works as expected when submitting the form, if I check the box it stays checked, if I uncheck it it stays unchecked.



Code:
Controller

    $data['MyCheckbox'] = true;
    $data['MyInput'] = 'Hello world';
    
    $this->load->helper('form');
    $this->load->library('form_validation');
    
    $this->form_validation->set_rules('MyCheckbox', 'MyCheckbox');
    $this->form_validation->set_rules('MyInput', 'MyInput', 'required');
    
    if ($this->form_validation->run() == FALSE){
    
        $this->load->view('form_view', $data);
    
    }else{
    
        //process data.

    }


Code:
View

    echo '<p>';
    echo form_label('My Checkbox', 'MyCheckbox');
    $data = array(
        'name'=>'MyCheckbox',
        'id'=>'MyCheckbox',
        'value'=>'1',
        'checked'=>set_checkbox('MyCheckbox', $MyCheckbox)
    );
    echo form_checkbox($data);
    echo '</p>';
    
    echo '<p>';
    echo form_label('My Input', 'MyInput');
    $data = array(
              'name' =>'MyInput',
              'id'=>'MyInput',
              'value'=>set_value('MyInput', $MyInput),
              'maxlength'=>'50',
              'size' =>'50'
    );
    echo form_input($data);
    echo form_error('MyInput', '<span class="formError">', '</span>');
    echo '</p>';




Theme © iAndrew 2016 - Forum software by © MyBB