如何使用codeigniter将复选框值写入数据库为1/0而不是/ 0

时间:2013-06-25 10:48:38

标签: php codeigniter activerecord types checkbox

尝试在我的数据库中保存复选框'已检查状态。我正在使用codeigniter。我将cehckbox表单值添加到数组中,如下所示: 'treated' => $this->input->post('treated'.$i),

然后我写入数据库传递包含上述输入的数组。

这是以'on'的形式写入数据库并与char/varchar一起使用。但是,如果我将其更改为bittinyint(1),则会因mssql错误Conversion failed when converting the varchar value 'on' to data type tinyint.

而失败

如何将选中的复选框值作为1而不是on传递?

提前致谢。

更新

 $i=1;
    while($i<=$this->input->post('orderlines'))
  {
$treatedVal=$this->input->post('treated'.$i)?1:0;

      //set the data for line insert start
        $data = array(
      'treated' => $treatedVal,    
        );

        $this->sales_model->order_lines_insert($data);  
  $i++;
  }

3 个答案:

答案 0 :(得分:4)

您应该在HTML中更改它。

<html>
    <input type="checkbox" name="check" value="1" />  <!--give it a value attribute the default is "on"-->
</html>

答案 1 :(得分:1)

在创建数据数组之前简单地执行此操作

 $treatedVal=$this->input->post('treated'.$i)?1:0;
 // then add this variable value to your array
 'treated' => $treatedVal,

答案 2 :(得分:0)

我就是这样做的:

var myVal = ( $('#myCheckbox').is(':checked') ) ? 1 : 0;

alert('myVal is: ' +myVal); //alerts 1 if checked, 0 if not checked

jsFiddle Demo