0
<input type="checkbox" name="chkbx" value="true" checked>

$data['active'] = $this->input->post('active');

        if($data['active'] ==  true){
            echo $data['active'] = 1 ;
        }
        else{
            echo $data['active'] = 0 ;
        }

It always put 0 value in database. I can't understand.
please help me to solve this problem.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

3 Answers3

1

Checkboxes are posted only when they are checked.

Use isset()

Use proper checkbox name.

$data['active'] = $this->input->post('chkbx');
if (isset($data['active'])) {
 echo $data['active'] = 1;
}
else {
 echo $data['active'] = 0;
}
Pupil
  • 23,834
  • 6
  • 44
  • 66
0

According to docs "The main advantage of using the provided functions rather than fetching an item directly ($_POST['something']) is that the functions will check to see if the item is set and return false (boolean) if not".

try using

if($data['active'] !==  false){

        // Check box value is set check its value and insert in DB here
    }
    else{
       // Check box not set you can have your fall back method
    }
Sachin G.
  • 1,870
  • 19
  • 24
0

Your checkbox name name="chkbx" and $this->input->post('active') is different. Use the same and check.

Sharmistha Das
  • 175
  • 1
  • 9