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);
}