I have implemented a function in javaScript that takes the indicated value and then queries it with php in the database but when I came back with a php JSON and read it in javaScript I can not get the data
JavaScript Code:
function capturar(){
var celular = document.getElementById("celular").value;
$.post("php/listarInformes.php", { celular: celular}, function(data){
var datos = data;
alert(datos.nombres);
});
}
PHP Code:
<?php
session_start();
include "conexion.php";
include "funciones.php";
if(isset($_POST["documento"]) || isset($_POST['celular'])){
$datos='';
if(!empty($_POST['celular'])){
//INFORMES
$sql = "SELECT * FROM informes WHERE celular='".$_POST['celular']."';";
$resultado = mysqli_query($conexion,$sql);
$fila = mysqli_fetch_array($resultado,MYSQLI_ASSOC);
if(empty($fila['codigo'])){
//CLIENTE
$sql = "SELECT * FROM cliente WHERE celular='".$_POST['celular']."';";
$resultado = mysqli_query($conexion,$sql);
$fila = mysqli_fetch_array($resultado,MYSQLI_ASSOC);
if(empty($fila['codigo'])){
}else{
//CLIENTE
$datos=array(
"documento"=>"".$fila['dni']."",
"nombres"=>"".$fila['nombres']."",
"apellidos"=>"".$fila['apellidos']."",
"fecha de nacimiento"=>"".$fila['fechaNacimiento'].""
);
}
}else{
//INFORMES
$datos=array(
"documento"=>"".$fila['dni']."",
"nombres"=>"".$fila['nombres']."",
"apellidos"=>"".$fila['apellidos']."",
"fecha de nacimiento"=>"".$fila['fechaNacimiento'].""
);
}
}
echo json_encode($datos, JSON_PRETTY_PRINT);
}
?>