0

I am currently working on a program that uses php jquery and bootstrap, I want to delete multiple data using checkbox. I search and found out that it must use AJAX to passing the paramater from view to controller. The Jquery work find and can get the value from checkbox. But when I use Ajax, the button not working. Nothing change. Please help me to find the problem on my code. Thank you

My Controller

public function hapusbeberapa()
    {
        $this->model_security->getsecurity();
        $this->load->model('model_barang');
        $arr = $this->input->post('data');

        //$arrcode= json_encode($arr);
        foreach($arr as $id)
            {
                $query = $this->model_barang->getID($id);
                if($query->num_rows()>0)
                {
            $this->model_barang->getdelete($id);
            //$this->session->set_flashdata('info',"Data Berhasil Dihapus");
                } 
            }
    }

My Model

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_barang extends CI_model {

    public function getdelete($key)
    {
        $this->db->where('IDBarang',$key);
        $this->db->delete('tBarang');

    }

    public function getID($key)
    {
        $this->db->where('IDBarang',$key);
        $query = $this->db->get('tbarang'); 
        return $query;
    }
}

My View

<script src="<?php echo base_url();?>http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<script type="text/javascript" src="<?php echo base_url();?>http://code.jquery.com/jquery.js"></script>

<script type="text/javascript">

    $(document).ready(function() {

        $("#btnhapus").click(function(){

            var favorite = [];

            $.each($("input[name='checkboxlist']:checked"), function(){            

                favorite.push($(this).val());

            });

                $.post("<?php echo site_url(barang/hapusbeberapa) ?>"+actMult, {data:favorite}););

        });

    });

</script>

</script>
2
  • what is actMult in the url you're passing in $.post(); Commented Jul 18, 2016 at 5:41
  • I just following a tutorial from link Commented Jul 19, 2016 at 4:48

2 Answers 2

0

use your script as:

<script type="text/javascript">

$(document).ready(function() {

    $("#btnhapus").click(function(){

        var favorite = [];

        $.each($("input[name=checkboxlist]:checked"), function(){            

            favorite.push($(this).val());

        });

            $.post("<?php echo site_url(/barang/hapusbeberapa) ?>", {data:favorite}, function(returnData){
            console.log(returnData);
      });

    });

});

</script>

it will work for sure, the result will be same as you echo in controller's function, and check console log for check result, you can also check it by using alert(returnData) instead of console.log(returnData).

hope it helps

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

2 Comments

I change the code as you told me, but nothing happend. The web only show the confirmation dialog and then the data is still there. This also happend when I use AJAX or jquery to update the data
its could be because you didn't echo any message in controller
0

Try something like that:

var favorite = [];
$.each($("input[name='checkboxlist']:checked"), function(){            
   favorite.push($(this).val());
});
favorite = favorite.join(); 
$.post
...

In Controller

$arr = $this->input->post('data');
$arr = explode(',', $arr);

1 Comment

why explode, data is already an array, so we don't need

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.