views:

75

answers:

2

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

A: 

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

andreas
Actually I have tried to explode $ids = explode(',',$_POST['ids']); but still not work. It always reported succes but do nothing and data still in table database.
oktar
It seem the error is in data: {id:idStr}, Undefined index: ids
oktar
Sorry,Undefined index:idStr in ticket/delete2
oktar
A: 

use json encode and decode for serialized data transfer

zod