使用 ajax 更新数据和获取数据

update data and GET data with ajax

我尝试通过模态 Bootstrap 显示数据并使用 ajax 更新数据但是不能一起完成,如何能够在 codeigniter 上显示数据和更新数据?

我的观点ajax

<a href="javascript:void(0)" onclick="show_note('<?php echo $row->id_note; ?>');">unread</a>

function show_note(id_note) {
    $.ajax({
        url : "<?php echo site_url('my_controll/note_update')?>/" + id_note,
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {
            $('#note').val(data.note);
            $('#note_show').modal('show'); 
        },
        error: function (jqXHR, errorThrown)
        {
            alert('Error ajax');
        }       
    });     
}

控制器

function note_update($id_note) {    
    $data = $this->M_model->detail_note($id_note);
    echo json_encode($data);        
}

模特

function detail_note($id_note) {    
    $this->db->set('status_note', '0');
    $this->db->where('id_note', $id_note);              
    $this->db->update('table_note', $data);  

    $query = $this->db->select('*')
                      ->from('table_note')
                      ->where('id_note', $id_note)
                      ->get();
    return $query->row();                   
}

你试过这样吗..

控制器:

function note_update($id_note) {    
    $data = $this->M_model->detail_note($id_note);
    echo json_encode($data);        
}

型号:

function detail_note($id_note) {    
    $this->db->set('status_note',0);
    $this->db->where('id_note', $id_note);              
    $this->db->update('table_note');  

    $query = $this->db->select('*')
                      ->from('table_note')
                      ->where('id_note', $id_note)
                      ->get();
    return $query->row_array();                   
}

别忘了加载 url 助手和你的 ajax 必须是这样的..

<a href="javascript:void(0)" onclick="show_note('<?php echo $row->id_note; ?>');">unread</a>

function show_note(id_note) {
    $.ajax({
        url : "<?php echo base_url('my_controll/note_update');?>/"+id_note,
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {
            var data = JSON.parse(data);
            $('#note').val(data.note); 
        },
       complete: function(data){
         $('#note_show').modal('show');
         },
        error: function (jqXHR, errorThrown)
        {
            alert('Error ajax');
        }       
    });     
}