Codeigniter检查复选框值 [英] Codeigniter checking checkbox value

查看:99
本文介绍了Codeigniter检查复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是CodeIgniter,我有一个带有复选框的表单,允许用户检查和删除他们上传的照片。

在我的控制器中,我使用 if(isset($ checked)== 1)来检查用户是否要删除照片。

$ photo_to_table 将设置为空,并传递到 $ this-> tank_auth-> update_user c $ c>执行数据库更新,并在表中将照片字段设置为空。



但在我的代码中,无论我是否检查,当我点击 UPDATE 按钮,它会一直删除照片,我不知道为什么会发生?



有人可以通过我的代码, / p>

控制器

  $ user_id = $ this-> session-> userdata('user_id'); 
$ profile = $ this-> users-> get_profile_by_id($ user_id);

if(!empty($ _ FILES ['photo'] ['name']))
{
//图片上传过程
}
else
{
$ checked = $ this-> input-> post('remove');
if(isset($ checked)== 1)
{
$ photo_to_table ='';
//这里将删除在目录中删除文件的过程
}
else
{
$ photo_to_table = $ profile->
}
}

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 ,
$ this-> form_validation-> set_value('website'),
$ photo_to_table
)))
{
// success
$ this-> session-> set_flashdata('msg','个人资料已更新');
redirect(current_url());
}
}

查看: p>

 <?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 =removetype =checkboxname =remove>
< label for =remove>删除照片< / label>
< / div>
< / td>
< / tr>
<?php
}
?>

谢谢。

解决方案

这里你需要做的是改变这一行...

  if(isset )== 1){

  if((int)$ checked == 1){

原因是变量$ checked将始终设置它的值是否为1。 $ checked = $ this-> input-> post('remove'); 将返回NULL,如果'remove'未在POST数据中设置。


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.

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?

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.

解决方案

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.

这篇关于Codeigniter检查复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆