代码点火器MVC ajax删除不起作用

时间:2018-01-03 16:58:49

标签: php jquery codeigniter

为什么会失败。
 我的观点代码

function Dispose(id){

if(confirm('Are you sure delete this data?'))
    {
        // ajax delete data to database
            $.ajax({
            url:"<?php echo site_url('Login/ajax_delete')?>" , 
            type: 'post',
            data: {'id' : id},
            success: function () {
                alert('ajax success');
            },
            error: function () {
                alert('ajax failure');
            }
        });

    }    
}

控制器代码:

 public function ajax_delete()
    {

        $id = $this->input->post('id');
        $this->mod->delete_by_id($id); 

    }

型号代码:

public function delete_by_id($id)
            {
                $this->db->where('id', $id);
                $this->db->delete('chemicalBottleInfo');
            }

我想要发生的是从chemicalbottleinfo

中删除该项目

2 个答案:

答案 0 :(得分:0)

你传递的id错误不应该是引号

function Dispose(id){

if(confirm('Are you sure delete this data?'))
    {
        // ajax delete data to database
            $.ajax({
            url:"<?php echo site_url('Login/ajax_delete')?>" , 
            type: 'post',
            data: {id : id},
            success: function () {
                alert('ajax success');
            },
            error: function () {
                alert('ajax failure');
            }
        });

    }    
}

答案 1 :(得分:0)

另一种方式:

  • 在删除表格中的ID之前添加支票。

    Javascript:

function Dispose(id){
            if(confirm('Are you sure delete this data?'))
                {
                    // ajax delete data to database
                        $.ajax({
                        url:"<?php echo site_url('Login/ajax_delete/'+id)?>" , 
                        success: function () {
                            alert('ajax success');
                        },
                        error: function () {
                            alert('ajax failure');
                        }
                    });
                }  
        }

控制器

public function ajax_delete($id= '')
{
    $id = $this->input->post('id');
     if($id != ''){
     $response = $this->usrExist($id, 'chemicalBottleInfo');
    }
   if($response == 1){
     $this->mod->delete_by_id($id); 
   }

}

public function usrchk($id,$tableName) {
        $qry = "SELECT count(*) as cnt from ".tableName." where id= ?";
        $res = $this->db->query($qry,array($id))->result();
        if ($res[0]->cnt > 0) {
           return 1;
        } else {
           return 0;
        }
    }