trying to save the checkbox 'checked status in my database. I am using codeigniter for this. I add the cehckbox form value to the array as follows: 'treated' => $this->input->post('treated'.$i),
I then write to database passing my array which contains the above input.
this is written to the database as 'on' and works with char/varchar
. if however I change this to bit
or tinyint(1)
it fails with mssql error Conversion failed when converting the varchar value 'on' to data type tinyint.
How can I pass the checked checkbox value as 1
instead of on
?
Thanks in advance.
update
$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++;
}
You should change it in the HTML itself.
<html>
<input type="checkbox" name="check" value="1" /> <!--give it a value attribute the default is "on"-->
</html>
simply do this before creating data array
$treatedVal=$this->input->post('treated'.$i)?1:0;
// then add this variable value to your array
'treated' => $treatedVal,
This is how I do it:
var myVal = ( $('#myCheckbox').is(':checked') ) ? 1 : 0;
alert('myVal is: ' +myVal); //alerts 1 if checked, 0 if not checked