php Codeigniter 检查复选框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18997782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter checking checkbox value
提问by conmen
I'm using CodeIgniter and I have a form with a checkbox that allows the user to check and remove their uploaded photo.
In my controller I use if(isset($checked) == 1)
to check if the user wants to remove the photo. $photo_to_table
will set empty and pass to $this->tank_auth->update_user()
to perform db update, and set photo field to become empty in table. Otherwise, it will remain the same photo.
我正在使用 CodeIgniter,我有一个带有复选框的表单,允许用户检查和删除他们上传的照片。
在我的控制器中,我if(isset($checked) == 1)
用来检查用户是否想要删除照片。$photo_to_table
将设置为空并传递给$this->tank_auth->update_user()
执行数据库更新,并将表中的照片字段设置为空。否则,它将保持相同的照片。
But in my code, whether I check it or not, when I click UPDATE
button, it keeps removing the photo, I wonder why is it happening?
但是在我的代码中,无论我是否检查它,当我单击UPDATE
按钮时,它一直在删除照片,我想知道为什么会发生这种情况?
Can someone please go through my code and give an advise?
有人可以通过我的代码并给出建议吗?
Controller:
控制器:
$user_id = $this->session->userdata('user_id');
$profile = $this->users->get_profile_by_id($user_id);
if(!empty($_FILES['photo']['name']))
{
//image upload process
}
else
{
$checked = $this->input->post('remove');
if(isset($checked) == 1)
{
$photo_to_table = '';
// here will do remove process to delete file in directory
}
else
{
$photo_to_table = $profile->photo;
}
}
if($this->form_validation->run()) { // validation ok
if(!is_null($data = $this->tank_auth->update_user(
$this->form_validation->set_value('name'),
$this->form_validation->set_value('country'),
$this->form_validation->set_value('website'),
$photo_to_table
)))
{
// success
$this->session->set_flashdata('msg', 'Profile has been updated');
redirect(current_url());
}
}
View:
看法:
<?php echo form_open_multipart($this->uri->uri_string()); ?>
<table border="0">
<?php
if(!empty($uphoto)){
?>
<tr>
<td>
<div>
<img src="<?php echo base_url().$userpath.$uphoto; ?>" />
</div>
<div>
<input id="remove" type="checkbox" name="remove">
<label for="remove">Remove photo</label>
</div>
</td>
</tr>
<?php
}
?>
Thanks.
谢谢。
回答by TigerTiger
What you need to do here is to change this line ...
你在这里需要做的是改变这一行......
if(isset($checked) == 1){
to
到
if((int) $checked == 1){
The reason is that the variable $checked will always be set whether its value is 1 or not. $checked = $this->input->post('remove');
will return NULL if the 'remove'
is not set in the POST data.
原因是变量 $checked 将始终设置其值是否为 1。$checked = $this->input->post('remove');
如果'remove'
未在 POST 数据中设置,则将返回 NULL 。
回答by Roopendra
Please write proper value in your checkbox :-
请在您的复选框中填写正确的值:-
<input id="remove" type="checkbox" name="remove">
Write some value then check it :-
写一些值然后检查它:-
for e.g :
例如:
<input id="remove" type="checkbox" name="remove" value="1">
In php :-
在 PHP 中:-
if($checked == 1) {
// do whatever u want
}
回答by Nil'z
Try:
尝试:
<input id="remove" type="checkbox" name="remove" value="1">
回答by Jhonathan H.
remove isset
消除 isset
its because by default in your CI Controller you get input value using
这是因为默认情况下,在您的 CI 控制器中,您使用
$checked = $this->input->post('remove');
$checked = $this->input->post('remove');
whether is has a value or not your variable now exist..
是否有值,您的变量现在是否存在..
回答by Olizy
Use this if it can help.
如果有帮助,请使用它。
$checked = (isset($_POST['checkbox']))?true:false;