简体   繁体   English

我如何使用Codeigniter在Ajax中调用函数进行更新

[英]How do I call a function within ajax using codeigniter to update

I want to call a model function within ajax to update the data the should be in the database. 我想在ajax中调用模型函数来更新应该在数据库中的数据。

My main function controller: 我的主要功能控制器:

function m_addR(){
    $this->load->view('m_add_view');
}

this other function in my controller is the one that I want to call from my m_add_view to edit data in the database with ajax while remaining in my m_add_view. 我控制器中的另一种功能是我想从我的m_add_view中调用的那个功能,以使用ajax编辑数据库中的数据,同时保留在我的m_add_view中。

what I am thinking is that it should execute the function m_update without leaving m_add_view view as to not have to reload the page. 我在想的是它应该执行功能m_update而不离开m_add_view视图,因为不必重新加载页面。

function m_update($id, $text, $column_name){
    $data = array('text'=>$text);
    $this->db->where('id', $id);
    $this->db->update($column_name, $data);
}

this is the code that I have so far to update is: 这是我到目前为止要更新的代码是:

function edit_data(id, text, column_name){
    $.ajax({
        url:"<?php echo base_url(); ?>control/m_update",
        method:"POST",
        data:{
            id:id,
            text:text,
            column_name:column_name
        },
        dataType:"text",
        success:function(data){
            /*alert(data);*/
            $('#infomsg').show();
        }
    });
}

I want to know how to send id, text and colum_name to my m_update function to update the database behind the scenes. 我想知道如何将id,text和colum_name发送到我的m_update函数,以在后台更新数据库。

Figured out how to update it. 弄清楚如何更新它。

function m_update(){
    $id = $this->input->post('id');
    $text = $this->input->post('text');
    $column_name = $this->input->post('column_name');
    $data = array($column_name => $text);
    $this->db->where('id', $id);
    $this->db->update('table_name', $data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM