• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checkbox value never posted?

#1
[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!

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

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

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.