我正在尝试通过ajax删除cakephp数据。我已将postlink按钮更改为简单的html按钮,如
<button class="del" id=<?php echo $user['User']['id']; ?>>Delete </delete>
现在,我可以通过以下代码获取用户ID
$('document').ready(function(){
$('.del').click(function(){
var x=$(this).attr("id");
alert(x);
});
});
我已成功获取用户的ID。现在我正在尝试将其发送给用户控制器中的删除操作。所以我编码为
$('document').ready(function(){
$('.del').click(function(){
var x=$(this).attr("id");
alert(x);
jQuery.get("<?php echo $this->webroot . $this->params["users"]; ?>/delete",{"id":x},function(data,stat){
jQuery("#success").load("success.ctp");
});
});
});
现在在控制器的删除操作中我试过了
public function delete($id=NUll) {
$id=$_GET['id'];
$this->User->id = $id;
if ($this->User->delete()) {
$this->Session->setFlash(__('The user has been deleted.'));
}
return $this->redirect(array('action' => 'index'));
}
这里它不起作用。在控制器中如何定义它?
答案 0 :(得分:0)
如果您在过滤前使用
function beforeFilter()
{
//Here set Which pages should be accessable to various users
$adminPages =array('delete');
$this->Auth->allow($adminPages);
......
}
允许使用Auth allow执行它。
如果那也正常,请检查AJAX字符串是否正确。使用MANUAL DELETE检查abc.com/Users/delete/id:2
只需复制粘贴字符串即可进入jQuery.get()
。它是否正确删除?
如果一切正常,请按以下步骤操作:)
public function delete($id=NUll) {
$id=$_GET['id'];
$this->User->id = $id;
if ($this->User->delete()) {
$this->Session->setFlash(__('The user has been deleted.'));
}
return $this->redirect(array('action' => 'index'));
}
和ajax:
jQuery.get("<?php echo $this->webroot . $this->params["users"].'/delete/';?>"+x+"",{"id":x},function(data,stat){
jQuery("#success").load("success.ctp");
});
答案 1 :(得分:0)
这里错误只是网址,我已经改变了它,现在它正常工作
echo $this->webroot . $this->params["users"]; ?>/delete"
TO
echo Router::url(array('controller'=>'users','action'=>'delete'));?>"
答案 2 :(得分:0)
如果您想使用ajax
,请尝试此操作 public function delete_user() {
$this->autoRender = false;
if ($this->request->data) {
$this->User->id = $this->request->data['id'];
$update = $this->User->saveField($this->request->data['field'], $this->request->data['value']);
if ($update) {
$repsonce['success'] = '1';
$this->Session->setFlash(__d('User', 'User Deleted successfully'), 'default', array('class' => 'alert alert-success'));
} else {
$repsonce['success'] = '0';
}
} else {
$repsonce['success'] = '0';
}
echo json_encode($repsonce);
}