2016-07-31 77 views
1

我有一个控制器将数据发送到模型,模型将这些数据插入到mysql中。如何接收来自codeigniter控制器的ajax响应?

我想知道插入的行的最后一个ID,但我希望在我的ajax函数中使用此ID来提升表中的信息。

在这里我有什么:

型号:

public function add($nome, $documento) 
    { 
    $dados = array (
       'nome' => $nome, 
       'documento' => $documento 
    ); 

    $this->db->insert('clientes', $dados); 
    return $this->db->insert_id(); 
    } 

控制器:

public function add() 
{ 
    // validar 

    $nome = $this->input->post('inputNome'); 
    $documento = $this->input->post('inputDocumento'); 
    $this->Cliente_model->add($nome, $documento); 
    return "ok"; 
} 

Ajax的功能:

  $(document).ready(function(){ 
      $("#salvarCliente").click(function(){ 
         $.ajax({ 
          url: "cliente/add", 
          type: 'POST', 
          data: $("#formCliente").serialize(), 
          success: function(msg){ 
           alert(msg); 
           $("#clienteMensagem").html('Cliente cadastrado com sucesso!'); 
           $("#table-clientes tr:last").after('<tr><td>'+msg+'</td><td>' + $('#clienteNome').val() + '</td><td>' + $('#clienteDocumento').val() + '</td><td></td></tr>'); 
           $("#clienteNome").val(''); 
           $("#clienteDocumento").val(''); 
          } 
         }); 
        return false; 
       }); 
     }); 

的代码我的数据添加到MySQL,但我看不到我的console.log中的控制器的“ok”或我发送数据前的浏览器中的警报。

我只想将“$ this-> db-> insert_id()”的结果从我的模型返回到我的控制器,并从我的控制器返回到我的ajax函数。

回答

2

变化如下:

控制器:

public function add() 
    { 
     // validar 

    $nome = $this->input->post('inputNome'); 
    $documento = $this->input->post('inputDocumento'); 
    $res = $this->Cliente_model->add($nome, $documento); 
    echo json_encode($res); 
} 

Ajax的功能:

 $(document).ready(function(){ 
     $("#salvarCliente").click(function(){ 
        $.ajax({ 
         url: "cliente/add",//Enter full URL 
         type: 'POST', 
         dataType:"JSON" 
         data: $("#formCliente").serialize(), 
         success: function(msg){ 
          alert(msg); 
          $("#clienteMensagem").html('Cliente cadastrado com sucesso!'); 
          $("#table-clientes tr:last").after('<tr><td>'+msg+'</td><td>' + $('#clienteNome').val() + '</td><td>' + $('#clienteDocumento').val() + '</td><td></td></tr>'); 
          $("#clienteNome").val(''); 
          $("#clienteDocumento").val(''); 
         } 
        }); 
       return false; 
      }); 
    }); 
+0

我看到了你的第一个answear昨晚和工作过。谢谢老兄! –

+0

不客气的朋友@Ramon Carvalho – Nikhil

2

你无法看到 “OK”,因为在阿贾克斯的参数还没有设置数据类型。添加一个参数dataType:json/html,然后您将能够从控制器接收数据。 事情是这样的:

$.ajax({ 
    url: "cliente/add", 
    type: 'POST', 
    data: $("#formCliente").serialize(), 
    dataType: 'json' 
    success: function(msg){ 
      alert(msg); 
      } 
}); 

并更换控制器功能到这个

public function add() 
{ 
    // validar 
    $nome = $this->input->post('inputNome'); 
    $documento = $this->input->post('inputDocumento'); 
    $id = $this->Cliente_model->add($nome, $documento); 
    echo json_encode($id); 
} 
+0

谢谢,这也是工作! –

1
public function add() 
{ 
    // validar 

    $nome = $this->input->post('inputNome'); 
    $documento = $this->input->post('inputDocumento'); 
    $insert_id = $this->Cliente_model->add($nome, $documento); 
    echo $insert_id; exit; 
} 
+0

谢谢,这也是工作! –