Welcome Guest, Not a member yet? Register   Sign In
checkboxe value


it might be an easy one, but how do I pass or retrieve back checkbox value (0 or 1) from and to a database.


Aren't you answering your own question here? Save that 0 or 1 to a database field. In fact, just save the one and if there is no '1' then the checkbox is unchecked.

yes, i figured this out with the 0 and 1, but, I was actually looking for some sample code on how to do it in CodeIgniter.


If I remember correctly, you need to use isset on the checkbox when processing it, so either

In both cases, it returns 1 if its checked, and 0 otherwise

This is what I used to check the status of a checkbox submitted from a form. Be sure to include the name of the checkbox input in your form validation (though in my case I won't ever save/set the status of the checkbox in the form because it uses it to perform a specific function) if you are using that feature of your form (just FYI).

$photocb = $this->input->post('photocb'); # name of my checkbox field
if(isset($photocb) && $photocb !== FALSE){ # FALSE if no checkbox is checked...redundant, but safe
     foreach($photocb as $value){ # checks the status of multiple checkboxes, so can be removed
        $this->model->update_f('table_id',$value); # or however you update your table
NOTE: The if statement checks are probably redundant, but better safe than...

This is how the checkbox(es) field(s) is/are setup in the form:
     # etc.

thank you very much.

Theme © iAndrew 2016 - Forum software by © MyBB