Send a variable to the controller from AJAX in CodeIgniter [duplicated]

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);
  }
}
    
asked by FeRcHo 20.12.2016 в 23:50
source

2 answers

1

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 post request and in your controller in codeigniter what you expect is a get request

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 through 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 getID function 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);
  }
}
    
answered by 21.12.2016 в 14:12
0

Try async:false , and get success off:

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