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

[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
#2

[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.
#3

[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
#4

[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
#5

[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.
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB