php – Codeigniter检查复选框值
发布时间:2020-05-25 08:56:47 所属栏目:PHP 来源:互联网
导读:我正在使用CodeIgniter,我有一个带有复选框的表单,允许用户检查并删除他们上传的照片. 在我的控制器中,我使用if(isset($checked)== 1)来检查用户是否要删除照片. $photo_to_table将设置为空并传递给$this- tank_auth- update_user()以执行数据库更新,并将照片
|
我正在使用CodeIgniter,我有一个带有复选框的表单,允许用户检查并删除他们上传的照片.
但是在我的代码中,无论是否检查,当我点击UPDATE按钮时,它会不断删除照片,我想知道为什么会这样? 有人可以通过我的代码并给出建议吗? 控制器: $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());
}
}
视图: <?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
}
?>
谢谢. 你需要做的是改变这一行……if(isset($checked) == 1){
至 if((int) $checked == 1){
原因是变量$checked将始终设置其值是否为1. $checked = $this-> input-> post(‘remove’);如果未在POST数据中设置’remove’,则返回NULL. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
