我正在学习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);
};
}
任何人会帮我请。谢谢
您可以尝试对ID数组使用.join(','),然后在服务器端使用explode()来获取传递给脚本的ID数组。 – Andreas 2010-10-07 00:40:54
要小心 - 这看起来像SQL注射可能真的伤害你在这里。你不应该使用$ _GET数据而不消毒 – mark 2010-10-07 01:35:49
请使用'$ 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