0

I can't seem to find what is wrong with my code. I want to be able to delete records which have been checked in a table.

This is my VIEW:

       <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>

My ajax function which was called in the view:

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!'
      });
    }
  });
    
  });
}

My Controller:

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

}

Model:

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

There are no errors returned by firebug. I'm not sure what's wrong and I've tried changing my code based on the other questions of other users which were answered here in stack, but mine still doesn't work. Any help is much appreciated. I'm pretty new to Codeigniter and PHP. Thanks again in advance!

5
  • make sure that the ids that was selected was really sent on the server, check that first. Commented Nov 22, 2014 at 14:46
  • '<?= base_url() ?>category/remove_category/<?= $row->id ?>' and your controller "public function remove_category($id)" Commented Nov 23, 2014 at 0:35
  • @Ghost i already did that. Commented Nov 23, 2014 at 2:42
  • @Victor that would work, but i am using multiple delete so i can't just use @row->id. and i need it to pass through my ajax controller first. Thanks anyway! Commented Nov 23, 2014 at 2:43
  • Your question missed the Post data in your ajax Commented Nov 23, 2014 at 4:13

1 Answer 1

0

You should use a foreach loop to delete. In the Controller:

public function remove_category()
{

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.