[英]CodeIgniter on/off form_checkbox() button not sending $_POST data
I'd like some help please. 请给我一些帮助。 I have this form group: This is the view:
我有这个表单组:这是视图:
<?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(); ?>
This is the controller 这是控制器
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');
}
Basicly this works as an on/off switch button . 基本上,这可以用作开/关按钮 。 The problem is that when I click the submit button it's not sending the $_POST data of the field (
$this->input->post('visible')
) to my model in order to store it in the database. 问题是,当我单击提交按钮时,它没有将字段(
$this->input->post('visible')
)的$ _POST数据发送到我的模型,以便将其存储在数据库中。
Any ideas what wrong and how should fix it?? 任何想法有什么错,应该如何解决?
This has nothing to do with Codeigniter, it's just how browsers work, if a checkbox is unchecked no POST data is sent to the server. 这与Codeigniter无关,这只是浏览器的工作方式,如果未选中此复选框,则不会将POST数据发送到服务器。
So you can either check in the controller for $this->input->post('visible')
(it will return false if the checkbox is unchecheck and whatever you have in value
. 因此,您可以在控制器中签入
$this->input->post('visible')
(如果复选框为unchecheck且具有任何value
,它将返回false。
Or you can do a small hack and put an hidden input with the same name and the value as false
before you checkbox. 或者,您可以做一个小小的黑客,然后在复选框前放置一个具有相同名称和值为
false
的隐藏输入。
In your example you should put value="1"
on the checkbox and have a <?=form_hidden('visible', 0)?>
before the checkbox. 在您的示例中,应将
value="1"
置于复选框中,并在复选框前<?=form_hidden('visible', 0)?>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.