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

[eluser]stuffradio[/eluser]
What method do you use for check boxes? I just want them to have a value of true and false. If it's checked when the form is submitted, it'll say true in the database, if not it will be false.
#2

[eluser]marcoss[/eluser]
1 and 0.
#3

[eluser]stuffradio[/eluser]
That doesn't answer anything...
#4

[eluser]Colin Williams[/eluser]
Use 1 for the true value, and 0 for the false value. You can do a set(0,1) or enum(0,1) field in your DB if you'd like
#5

[eluser]Michael Wales[/eluser]
Code:
$input['my_checkbox'] = FALSE;
if (isset($this->input->post('my_checkbox'))) {
  $input['my_checkbox'] = TRUE;
}

Checkboxes have a value of 'on' if they are checked and they are not passed through at all if unchecked.
#6

[eluser]flojon[/eluser]
[quote author="Michael Wales" date="1215423869"]
Code:
$input['my_checkbox'] = FALSE;
if (isset($this->input->post('my_checkbox'))) {
  $input['my_checkbox'] = TRUE;
}
[/quote]

Or in one line (since $this->input->post will return false if the item is not set)
Code:
$input['my_checkbox'] = $this->input->post('my_checkbox') !== FALSE;

(I'm not sure isset will work.. Will it?)
#7

[eluser]Seppo[/eluser]
isset won't work on expressions, only does with variables
Code:
// you can't
isset($this->input->post('my_checkbox')); // PHP error
// You can
isset($fake_var = $this->input->post('my_checkbox')); // always true, it is false or the value submitted
//and the good old...
isset($_POST['my_checkbox']); // returns true if submitted, as expected
// or how you suggested
$this->input->post('my_checkbox') !== FALSE; // returns true if submitted
#8

[eluser]Noinx[/eluser]
You can use this code in your view when adding checkboxes:
Code:
<input type="hidden" name="myCheckbox" value="0" />
<?=form_checkbox('myCheckbox', '1', myValue)?>

By using the hidden field, the value will be 'zero' if the box is not checked. For me this is preferred over not getting anything back about the field.
#9

[eluser]Yash[/eluser]
[quote author="Michael Wales" date="1215423869"]
Code:
$input['my_checkbox'] = FALSE;
if (isset($this->input->post('my_checkbox'))) {
  $input['my_checkbox'] = TRUE;
}

Checkboxes have a value of 'on' if they are checked and they are not passed through at all if unchecked.[/quote]

This is incorrect

http://ellislab.com/forums/viewreply/380464/




Theme © iAndrew 2016 - Forum software by © MyBB