php – Codeigniter检查复选框值

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Codeigniter检查复选框值脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用CodeignITer,我有一个带有复选框的表单,允许用户检查并删除他们上传的照片.
在我的控制器中,我使用if(isset($checked)== 1)来检查用户是否删除照片.
$photo_to_table将设置为空并传递给$this-> tank_auth-> update_user()以执行数据库更新,并将照片字段设置为在表格中为空.否则,它将保持相同的照片.

但是在我的代码中,无论是否检查,当我点击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.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Codeigniter检查复选框值全部内容,希望文章能够帮你解决php – Codeigniter检查复选框值所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。