I am facing this issue that my checkbox col in Database is set on tinyint(1)
and default value in "0" the problem is when I "Checked" the checkbox and update through this code that have updated successfully and the database value changed "0" to "1" and when i again test the code this time to "Unchecked" the checkbox and update then nothing happening with the screen not display any error and not value update this time "1" to "0"...
Model:
public function updateCheckbox($table, $id, $data)
{
$this->db->where('membership_id', $id);
return $this->db->update($table, $data);
}
Controller:
public function edituserstatus($id)
{
$checkbox = isset($_POST['checkbox']) ? 1 : 0;
if($this->input->post()){
$data = array(
'isactive' => $checkbox,
);
$this->member_model->updateCheckbox('users', $id, $data);
$this->session->set_flashdata('success', 'Updated Successfully!');
redirect(base_url().'admin/edituserstatus/'.$id, 'refresh');
}else{
$data['editdata'] = $this->member_model->getUserDataforEdit($id);
$this->load->view('admin/edituserstatus', $data);
}
}
View
<form action="<?php echo base_url()."admin/edituserstatus/".$editdata->membership_id ?>" method="post">
<input type="checkbox" name="isactive" value="<?php echo $editdata->isactive; ?>" <?php echo ($editdata->isactive == 1 ? 'checked' : 0); ?> />
<br /> <br />
<button type="submit" class="btn btn-primary btn-sm">Update</button>
</form>
I was trying too much things from all over internet but didn't get any answer I hope that I will get answer from here... Thanks
Model
public function updateCheckbox($table,$data, $id) {
$this->db->where('id',$id);
$update=$this->db->update($table,$data);
return $update?true:false;
}
Controller
public function edituserstatus($id){
$data['status'] = $this->input->post('status');
$result = $this->member_model->updateCheckbox('users', $id, $data);
echo json_encode($result);
}
View
$(document).on('click',"input",function(){
var status = null;
var id = $(this).attr('id');
if(this.checked)
{
status = 1;
}
else
{
status = 0;
}
$.ajax(
{
type: "POST",
url : "<?php echo base_url(); ?>"+"admin/edituserstatus/"+id,
data: {'status':status},
success:function(data)
{
console.log(data);
},
});
});
<form role="form" method="post" action="<?php echo base_url('controller/update');?>">
<div class="form-group">
<label> Name </label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<input type="checkbox" value="1"> name="checkbox_value" <?php if(isset($formData) && $formData->checkbox_value==1){ echo "checked";} ?>>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
public function edit($id='')
{
$this->data['formData'] = $this->model->get_data_by_id($id);
$this->load->view('view_page', $this->data);
}
public function update($id=''){
if(isset($_POST)){
$checkbox_value = isset($_POST['checkbox_value']) ? 1 : 0;
$data = array(
'name' => $_POST['name'],
'checkbox_value' => $checkbox_value
);
$this->model->update($id,$data);
}
redirect('controller/edit');
}
public function get_data_by_id($id='')
{
$result = $this->db->select('*')->where('id', $id)->get('table_name')->row();
return $result;
}
public function update($id='',$data)
{
$this->db->where('id',$id);
$result = $this->db->update('table_name',$data);
return $result;
}