Css CodeIgniter打开/关闭表单\u复选框()按钮未发送$\u POST数据
我想要一些帮助。我有这个表格组: 这是一种观点:Css CodeIgniter打开/关闭表单\u复选框()按钮未发送$\u POST数据,css,codeigniter,button,uibutton,codeigniter-form-helper,Css,Codeigniter,Button,Uibutton,Codeigniter Form Helper,我想要一些帮助。我有这个表格组: 这是一种观点: <?php echo form_open('admin/posts/post/'.$post->id); ?> // other fields here... <div class="form-group"> <label for="" class="col-sm-2">Visible</label> <div class
<?php echo form_open('admin/posts/post/'.$post->id); ?>
// other fields here...
<div class="form-group">
<label for="" class="col-sm-2">Visible</label>
<div class="col-sm-10">
<div class="onoffswitch">
<?php
$visible = ($post->visible) ? $post->visible : $this->input->post('visible');
$visible_data = array(
'class' => 'onoffswitch-checkbox',
'id' => 'visible',
'checked' => ($visible == '1') ? true : false,
'name' => 'visible',
'value' => ($post->visible) ? $post->visible : $this->input->post('visible'),
);
?>
<?php echo form_checkbox($visible_data); ?>
<label class="onoffswitch-label" for="visible">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
</div>
</div>
// more fields ...
<?php echo form_close(); ?>
基本上,这是一个有效的方法。问题是,当我单击submit按钮时,它没有发送字段的$\u POST数据($this->input->POST('visible')
)
复制到我的模型,以便将其存储在数据库中
有什么想法吗?有什么错误以及应该如何修复吗?这与Codeigniter无关,只是浏览器如何工作,如果复选框未选中,则不会向服务器发送POST数据 因此,您可以在控制器中签入
$this->input->post('visible')
(如果复选框未选中,以及值中的任何内容,它将返回false)
或者你可以做一个小的修改,在复选框前放置一个与false
名称和值相同的隐藏输入
在您的示例中,您应该将value=“1”
放在复选框上,并在复选框前添加一个
public function post($id){
$this->data['post'] = $this->post_model->get($id);
$this->form_validation->set_rules($this->post_model->rules);
if ($this->form_validation->run() === true) {
var_dump($this->input->post('visible')); //not getting anything from the visible field
// store data in database and redirect
$this->post_model->save($id);
}
// load the view
$this->load->view('admin/post/edit');
}