CodeIgniter Forums
checkboxe value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: checkboxe value (/showthread.php?tid=17854)



checkboxe value - El Forum - 04-17-2009

[eluser]nir[/eluser]
Hello,

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

thanks,
nir


checkboxe value - El Forum - 04-17-2009

[eluser]squarebones[/eluser]
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.


checkboxe value - El Forum - 04-17-2009

[eluser]nir[/eluser]
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.

thanks


checkboxe value - El Forum - 04-17-2009

[eluser]Dam1an[/eluser]
If I remember correctly, you need to use isset on the checkbox when processing it, so either
if(isset($this->input->post('my_checkbox')))
or
if(isset($_POST['my_checkbox']))

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


checkboxe value - El Forum - 04-18-2009

[eluser]squarebones[/eluser]
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).

Code:
$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:
Code:
form_checkbox('photocb[]','photo0');
     [form_checkbox('photocb[]',photo1');
     # etc.



checkboxe value - El Forum - 04-20-2009

[eluser]nir[/eluser]
thank you very much.