我正面临这个问题,我的数据库中的复选框col在tinyint(1)
上设置,默认值在“0”,问题是当我“选中”复选框并更新已成功更新的代码时数据库值改为“0”为“1”,当我再次测试代码时,“取消选中”复选框并更新然后屏幕上没有任何事情发生任何错误而且没有值更新此次“1”到“0” ......
型号:
public function updateCheckbox($table, $id, $data)
{
$this->db->where('membership_id', $id);
return $this->db->update($table, $data);
}
控制器:
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);
}
}
查看
<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>
我在互联网上尝试了太多的东西,但没有得到任何答案我希望我能从这里得到答案...谢谢
答案 0 :(得分:0)
模型
public function updateCheckbox($table,$data, $id) {
$this->db->where('id',$id);
$update=$this->db->update($table,$data);
return $update?true:false;
}
控制器
public function edituserstatus($id){
$data['status'] = $this->input->post('status');
$result = $this->member_model->updateCheckbox('users', $id, $data);
echo json_encode($result);
}
查看
$(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);
},
});
});
答案 1 :(得分:0)
<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;
}