【问题标题】:Codeigniter:: deleting multiple records in checkboxCodeigniter:: 删除复选框中的多条记录
【发布时间】:2014-11-22 14:43:38
【问题描述】:

我似乎找不到我的代码有什么问题。我希望能够删除已在表中检查的记录。

这是我的观点:

       <table id="table" name="table" class="table table-bordered table-condensed table-striped table-primary table-vertical-center checkboxs">
              <thead>
                <tr>
                  <th style="width: 1%;" class="uniformjs"><input type="checkbox" /></th>
                  <th class="center">Item Category</th>
                  <th class="center" style="width: 90px;">Actions</th>
                </tr>
              </thead>
              <?php foreach($categorycontent->result() as $row): ?>
                <tr>
                  <th style="width: 1%;" class="uniformjs"><input type="checkbox" name="delete[]" value="<?php echo $row->id; ?>"/></th>
                  <td class="center"><?php echo $row->category_name; ?></td>
                  <td class="center">
                    <a href="#" class="btn-action glyphicons pencil btn-success" title="Edit" onClick="popedit('<?php echo $row->id; ?>', '<?php echo base_url(); ?>category/update_category/', 'category')"><i></i></a>
                    </td>
                </tr>
              <?php endforeach; ?>
            </table>

<script type="text/javascript">
  $(function()
  {

    popadd("#newcategory", "<?php echo base_url(); ?>category/create_category/", 'category');
    popdeletechecked("#deletecategory", "Deleting this record/s will delete all linked information.</br>Are you sure you want to delete it?",  "<?php echo base_url(); ?>category/remove_category/");
  });
</script>

我在视图中调用的 ajax 函数:

function popdeletechecked(buttonid, msg, url)
{
  $(buttonid).click(function(){
    
    bootbox.confirm(msg,'No', 'Yes', function(result) {

      if(result) {
        $.ajax({
          url     : url,
          type    : 'POST',
          success : function(){
            window.location = url;
          }
        });
      }
      else {
      $.gritter.add({// Doesn't work on page reload
        title: 'Deleting Cancelled!'
      });
    }
  });
    
  });
}

我的控制器:

public function remove_category()
{
    
    $checked = $this->input->post('delete');
    $this->category_model->delete_category($checked);
  
    redirect('category/read_category');

}

型号:

function delete_category($id)
{
  $this->db->where('id', $id);
  $this->db->delete('tblitemcategories');
}

firebug 没有返回错误。我不确定出了什么问题,我尝试根据堆栈中回答的其他用户的其他问题更改我的代码,但我的仍然无法正常工作。任何帮助深表感谢。我对 Codeigniter 和 PHP 还是很陌生。再次提前感谢!

【问题讨论】:

  • 确保被选中的id确实是在服务器上发送的,先检查一下。
  • '= base_url() ?>category/remove_category/= $row->id ?>' 和你的控制器“public function remove_category($id)”
  • @Ghost 我已经这样做了。
  • @Victor 可以,但我使用的是多次删除,所以我不能只使用@row->id。我需要它首先通过我的 ajax 控制器。还是谢谢!
  • 您的问题错过了 ajax 中的 Post 数据

标签: javascript php ajax codeigniter


【解决方案1】:

您应该使用foreach 循环来删除。在Controller

public function remove_category()
{

    $checked = $this->input->post('delete');
    foreach($checked as $val){
        $this->category_model->delete_category($val);
    }
    redirect('category/read_category');

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多