2010-10-07 177 views
0

我正在学习Cakephp,并且我一直试图使用复选框删除多个(选中的)记录,但仍然不成功。这里是我的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); 
      } 
    }); 

这里是代码控制器:

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


} 

任何人会帮我请。谢谢

+0

您可以尝试对ID数组使用.join(','),然后在服务器端使用explode()来获取传递给脚本的ID数组。 – Andreas 2010-10-07 00:40:54

+1

要小心 - 这看起来像SQL注射可能真的伤害你在这里。你不应该使用$ _GET数据而不消毒 – mark 2010-10-07 01:35:49

+3

请使用'$ this-> Ticket-> deleteAll(array('id'=>/* array of id * /))'来使用SQL注入预防和抽象Cake给出的您。你*不应该*写手工SQL,除非没有其他办法让Cake做你想做的事情。 http://book.cakephp.org/complete/1000/Models#Deleting-Data-1035 – deceze 2010-10-07 02:18:20

回答

0

您可以尝试对ID数组使用.join(','),然后在服务器端使用explode()来获取传递给脚本的ID数组。

例如

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

传(IDSTR),以Ajax调用

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

在服务器端:

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

OR

检查jquery.param()函数中jquery文档。应用并传递给IDS数组,然后将其传递给$ .ajax({});

注意:您正在使用POST和您所提供

+0

其实我试过爆炸$ ids = explode(',',$ _ POST ['ids']);但仍然无法工作。它总是报告成功,但什么也不做,数据仍然在表数据库中。 – oktar 2010-10-07 02:24:46

+0

它似乎错误是在数据中:{id:idStr},未定义索引:ids – oktar 2010-10-08 19:22:35

+0

对不起,未定义的索引:票证中的idStr/delete2 – oktar 2010-10-08 19:24:14

0

用于串行数据传输使用JSON编码和解码的代码无法获得HTTP方法

0

由于JSON编码不jQuery的默认支持,下载JSON Plugin for jQuery

您的JavaScript然后变成:

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

控制器

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

的RequestHandler组件可确保这是一个AJAX请求。这是可选的。

相应视图

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

祝你好运!