CodeIgniter Forums
Checkbox value never posted? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Checkbox value never posted? (/thread-55305.html)



Checkbox value never posted? - El Forum - 10-19-2012

[eluser]entropy[/eluser]
I have a form with a checkbox:

Code:
echo form_checkbox(array(
             'name'        => 'terms',
             'id'          => 'terms'
       ),'',False);

I will spare you the whole code. When I submit the form, the controller that receives the post always has the checkbox value FALSE whether it was checked or not.

Code:
if($this->input->get_post('terms'))
array_push($err,'true');
else array_push($err,'false');

The $err array (test array) has the value always set to FALSE and the strlen of the value set to 0.

It is as if the value of the checkbox never makes it to the submit-controller. How is this possible? I double-checked everything!


Checkbox value never posted? - El Forum - 10-19-2012

[eluser]entropy[/eluser]
Nevermind. I found the culprit. I have to pass the 'value'-parameter a value!

Code:
echo form_checkbox(array(
             'name'        => 'terms',
             'id'          => 'terms'
       ),'1',False);

Still learning Confused


Checkbox value never posted? - El Forum - 10-19-2012

[eluser]CroNiX[/eluser]
Your checkbox doesn't have a value, so nothing to send.
Code:
echo form_checkbox(array(
  'name'        => 'terms',
  'id'          => 'terms',
  'value'       => 1,
  'checked'     => FALSE
));

$checked = (int)$this->input->post('terms');  //will be 1 if checked, 0 if not (normally FALSE if not, but we forced it to a int)



Checkbox value never posted? - El Forum - 10-19-2012

[eluser]CroNiX[/eluser]
Or if you want it to repopulate the checked state on form submission if there is an error...

Code:
'checked' => (set_value('terms') == 1)



Checkbox value never posted? - El Forum - 10-19-2012

[eluser]entropy[/eluser]
[quote author="CroNiX" date="1350668473"]Or if you want it to repopulate the checked state on form submission if there is an error...

Code:
'checked' => (set_value('terms') == 1)
[/quote]

Thanks! Great tip! Big Grin