使用复选框多次删除

I am learning Cakephp and I've been trying to delete multiple (checked) record using checkbox, but still not success. here's my jQuery :

            var ids = [];
  $(':checkbox:checked').each(function(index){
   ids[index] = $(this).val();;
   alert(ids[index]);

  });

  //alert(ids);
  var formData = $(this).parents('form').serialize();


  $.ajax({
         type: "POST",
         url: "tickets/multi_delete",
         data:"id="+ids,
         success: function() {
     alert('Record has been delete');
    },
    error:  function(XMLHttpRequest, textStatus, errorThrown) {
       alert(XMLHttpRequest);
       alert(textStatus);
       alert(errorThrown); 
             }
   });

and here is code in controller :

function multi_delete() {
  $delrec=$_GET['id'];
  //debuger::dump($del_rec);
  foreach ($delrec as $id) {

   $sql="DELETE FROM tickets where id=".$id;

   $this->Ticket->query($sql);
  }; 


 }

anybody will help me please. thank

you could try a .join(',') on the array of IDs and then an explode() on the server side to get the array of IDs passed to the script.

e.g.

var idStr = ids.join(','); 

pass it (idStr) to the ajax call

$.ajax({
     type: "POST",
     url: "tickets/multi_delete",
     data: {id:idStr},
    //more code cont.

on the server side:

$ids = explode(',',$_POST['ids']);

OR

check the jquery.param() function in the jquery docs. Apply and to the IDS array and then pass it to $.ajax({});

Note: You are using POST and not GET HTTP METHOD in the code you provided

use json encode and decode for serialized data transfer

Since JSON encoding is not supported in jQuery by default, download the JSON Plugin for jQuery.

Your javascript then becomes:

$.ajax({
     type: "POST",
     url: "tickets/multi_delete",
     data: { records: $.toJSON(ids) },
     success: function() {
         alert('Records have been deleted.');
     },
});

In the controller:

var $components = array('RequestHandler');

function multi_delete() {
        if (!$this->RequestHandler->isAjax()) {
            die();
        }
        $records = $_POST['records'];
        if (version_compare(PHP_VERSION,"5.2","<")) {
            require_once("./JSON.php"); //if php<5.2 need JSON class
            $json = new Services_JSON();//instantiate new json object
            $selectedRows = $json->decode(stripslashes($records));//decode the data from json format
        } else {
            $selectedRows = json_decode(stripslashes($records));//decode the data from json format
        }
        $this->Ticket->deleteAll(array('Ticket.id' => $selectedRows));
        $total = $this->Ticket->getAffectedRows();
        $success = ($total > 0) ? 'true' : 'false';
        $this->set(compact('success', 'total'));
    }

The RequestHandler component ensures that this is an AJAX request. This is optional.

The corresponding view:

<?php echo '({ "success": ' . $success . ', "total": ' . $total . '})'; ?>

Wish you luck!