在codeigniter中使用复选框进行多次删除

时间:2013-09-05 07:04:46

标签: php codeigniter checkbox

请有人帮我..我想删除多个项目,并在代码点火器中选中复选框..

我做了什么:我的视图页面看起来像这样

<script type="text/javascript" src="<?php echo base_url(); ?>/js/jquery_1_8_1_min.js"></script>
<script language="javascript">
$(function(){
    $("#selectall").click(function () {
        $('.case').attr('checked', this.checked);
    });
    $(".case").click(function(){
        if($(".case").length == $(".case:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
    });
});
</script>

<?php
foreach($query as $row)
{
    ?>
    <tr><td><input name="checkbox[]" class="case" type="checkbox"  value="<?php print $row->reg_id; ?>"></td></tr>
    <?php 
}
?>

我的控制器代码:

function del($reg_id) //delete content
{
    $this->load->helper(array('form','url'));
    $this->load->model('registration');
    $query=$this->registration->delete($reg_id);
    $data['query']=$this->registration->cr_getall();
    $this->load->view('vw_register',$data);
}

这是我的模特:

public function del($reg_id)
{
    $this->load->database();
    $del=$this->db->delete('tbl_contctform',array('reg_id'=>$reg_id));
    if($del)
    {
        return $del;
    }
    else
    {
        return false;
    }
}

我知道我犯了一些错误。但我不知道如何清除它。

是否可以使用ajax。?请帮助我。

2 个答案:

答案 0 :(得分:1)

试试这个,

$(".case").on('click',function(){
    var chk=$('.case:checked').length ? true : false;// if length is > 0 then return true
    $('#selectall').prop('checked',chk);// if chk true then check the selectAll checkbox         
});

查看fiddle

您需要创建form submit并致电function of controller上的form submit

# Your Controller Function
function del($reg_id) //delete content
{
    $this->load->helper(array('form','url'));
    $this->load->model('registration');
    $checkbox=$this->input->post('checkbox');# Using Form POST method you can use whatever you want like GET
    $reg_id=$this->input->post('reg_id');# Using Form POST method
    $regId_array=array();
    foreach($checkbox as $key=>$checked)
    {
        if($checked!==FALSE)
            $regId_array[]=$reg_id[$key];
    }
    if(!empty($regId_array))# array of checked reg_id to delete
    {
        $query=$this->registration->del($checkbox,$id);# use del here not delete as your model says
    }
    else{
        die('No checkbox selected!!!');
    }

    $data['query']=$this->registration->cr_getall();
    $this->load->view('vw_register',$data);
}

# Your Model Function
public function del($reg_id)
{
    $this->load->database();
    $this->db->where_in('reg_id', $reg_id);# delete all reg_id in $reg_id array variable
    $this->db->delete('tbl_contctform');

    if($del)
    {
        return $del;
    }
    else
    {
        return false;
    }
}

你的查看应该是,

<?php
foreach($query as $row)
{
    ?>
    <tr><td>
        <input name="checkbox[]" class="case" type="checkbox" />
        <input name="reg_id[]" type="hidden"  value="<?php print $row->reg_id; ?>">
    </td></tr>
    <?php 
}
?>

答案 1 :(得分:0)

我改变了我的视图,控制器,模型......现在它正常工作

查看:

<form name="forms" action="delete_checkbox/" method="post">
    foreach($query as $row)
    {?>
    <tr>
        <td><input type="checkbox" name="forms[]" id="forms[]"
                   value="<?php print $row->reg_id; ?>"/>
    </tr?
<?php } ?>

控制器:

function delete_checkbox() {
    $dat = $this->input->post('forms');
    for ($i = 0; $i < sizeof($dat); $i++) {
        print_r($dat[$i]);
        $this->load->model('registration');
        $this->registration->delete_check($dat[$i]);
    }
    redirect('viewreg/showall', 'refresh');
}

它有效!!