Pass a variable to the controller from AJAX in CodeIgniter

2

I have the following problem, in a view I get the data of a user through a foreach and one of them is the code, which through a link, I execute a function that passed the same code as parameter:

<a  href="" onclick="cargarID(<?=$fila->Id_usuario?>)"><?=$fila->Id_usuario?></a></td>

The function sends the id to a method of my controller:

<script type="text/javascript">
    function cargarID(id= null){
        if(id) {
            $.ajax({
                  url:"http://localhost/sistema/usuario_consulta/getID/"+id,
                  type:"POST",
                  dataType: 'json',
                  success:function(respuesta){
                      window.location.href = "http://localhost/sistema/usuario_consulta/index";
                  }
            });
        }
    }
</script>

This is my controller, I do this because I need to send a parameter to a query that I have in the model.

function index()
{
    $this->load->view('guest/section');
    $datos['arrPerfil'] = $this->model_usuario->consulta();
    $this->load->view('user/usuario_view', $datos);
}

public function getID($id) 
{
    if($id) {
        $data = $this->model_usuario->consulta($id);
        echo json_encode($data);
    }
}

in the model I have the following code:

function consulta($id){

    $query = $this->db-> query("select Id_usuario, nombre, telefono from usuario where Id_usuario ='".$id."'");

    if ($query->num_rows() > 0) {
        return $query->result();
     }
   }

where I need that parameter to execute the query and then return it in an array in the controller index

function index()
{

    $datos['arrPerfil'] = $this->model_usuario->consulta();
    $this->load->view('user/usuario_view', $datos);
}
    
asked by FeRcHo 21.12.2016 в 15:00
source

1 answer

2

Assuming the following line works:

<a  href="" onclick="cargarID(<?=$fila->Id_usuario?>)"><?=$fila->Id_usuario?></a>

And the user_id is arriving correctly.

Your role

function cargarID(id= null){
      if(id) {
      $.ajax({
        url:"http://localhost/sistema/usuario_consulta/getID/"+id,
      type:"POST",
      dataType: 'json',
      success:function(respuesta){
         window.location.href = "http://localhost/sistema/usuario_consulta/index";
       }
     });
    }
   }

It's wrong in the part of the ajax call, specifically in:

type:"POST",

You are sending a request post and in your controller in CodeIgniter what you expect is a request get

public function getID($id) 
{
  if($id) {
    $data = $this->model_usuario->consulta($id);
    echo json_encode($data);
  }
} 

I do not know what your query returns in model_usario but there you should return an array that you can then use with jquery or javascript.

What you could do is change the type of request in the ajax call from POST to GET , or better I would recommend that you do it using POST and also change your controller in codeigniter, so that you have more control of the data you send.

For example, your ajax call would be:

function cargarID(id){
   if(id != null) {
         $.ajax({
            url:"http://localhost/sistema/usuario_consulta/getID",
            type:"POST",
            dataType: 'json',
            data: {'id_persona': id},
            success:function(respuesta){
             window.location.href = "http://localhost/sistema/usuario_consulta/index";
           }
         });
   }
}

And your function getID in the controller would be:

public function getID() 
{
  $id = $this->input->post("id_persona");
  if($id != null) {
    $data = $this->model_usuario->consulta($id);

    header('Content-Type: application/json');
    echo json_encode($data);
  }
}

EDIT

I do not understand what you want to do, so you use ajax if you are not going to use that id in that call, the problem you have is that you do not get the id to the index function of your controller, if you only want that you could do it directly in your link.

for example

<a  href="<?php echo site_url()."/usuario_consulta/index/". $fila->Id_usuario;?>"> <?=$fila->Id_usuario?> </a>

Then your index function would be like this:

function index($id){

    $datos['arrPerfil'] = $this->model_usuario->consulta($id);
    $this->load->view('user/usuario_view', $datos);
}
    
answered by 21.12.2016 / 15:12
source