Read PHP Json data brought by ajax to Js

1

I send a variable by means of ajax to a php that consults me a unique record according to the id sent in mentioned ajax, I must bring all the data of that record, here the Js

function actualizar_cuenta()
{
  var selector = document.getElementById("select_cuenta_encontrada");
  //capturo el id de la cuenta el cual ya previamente esta configurado en el select_cuenta_encontrada
  var id_cuenta = selector.options[selector.selectedIndex].id;
  //Primero verificamos que si se haya escogido una cuenta y no "- seleccione -" en option del selectedIndex}
  if(id_cuenta=="seleccione")
  {
    alert("debe escoger una cuenta del cliente");
  }
  else
  {
   $.ajax({
   url: "../PHP/consultar_datos_de_cuenta.php",
    type: "POST",
    dataType:'json',
    data:({id_cuenta: id_cuenta}),
    }).done(function(respuesta)
    {
      var nombre=respuesta[1];
      alert(nombre); //aqui pruebo y me imprime "undefined"
     });
  }
}

here the PHP consultar_datos_de_cuenta.php

<?php
include "conectar.php";
$nombre = $_POST['nombre'];
//$sql = "SELECT id,valor_recibido FROM cuentas WHERE nombre_cliente=".$nombre;
$sql = "SELECT id,valor_recibido FROM cuentas WHERE nombre_cliente='$nombre'";

$result = mysqli_query($conexion,$sql);

/*ACA CREO UN ARRAY EL CUAL ME RECOGE LOS RESULTADOS DE LA CONSULTA*/
$array_resultado = mysql_fetch_row($result);

/* CONVIERTO MENCIONADO ARRAY EN FORMATO JSON PARA LUEGO LEERLO EN EL actualizar_cuenta.js*/

echo json_encode($array_resultado);

?>

If I press F12 in the browser and go to Network, php throws me the following, so I see that the query is doing well and is doing the echo that should be

  

[{0: "86", 1: "JAIRO MARTINEZ", 2: "56565", 3: "332", 4: "1", 5: "1", 6: "3614", 7: "6", 8: "5", ...}]   0   :   {0: "86", 1: "JAIRO MARTINEZ", 2: "56565", 3: "332", 4: "1", 5: "1", 6: "3614", 7: "6", 8: "5", ...}   0   :   "86"   one   :   "JAIRO MARTINEZ"   two   :   "56565"   3   :   "332"   4   :   "one"   5   :   "one"   6   :   "3614"   7   :   "6"   8   :   "5"   9   :   "6345"   10   :   "6"   eleven   :   "16"   12   :   "64"   13   :   "0"   14   :   "0"   fifteen   :   "0"   16   :   "0"   17   :   "0"   18   :   "0"   19   :   "0"   twenty   :   "0"   twenty-one   :   "0"   22   :   "eleven"   2. 3   :   "one"   24   :   "65"   25   :   "456"   26   :   "634"   27   :   "612"   28   :   "146"   29   :   "46"   30   :   "0"   31   :   "0"   32   :   "0"   33   :   "0"   3. 4   :   "0"   35   :   "0"   36   :   "0"   37   :   "0"   38   :   "0"   39   :   "0"   40   :   "DO NOT"   41   :   "DO NOT"   42   :   "DO NOT"   43   :   "DO NOT"   44   :   "DO NOT"   Four. Five   :   "DO NOT"   46   :   "DO NOT"   47   :   "DO NOT"   48   :   "DO NOT"   49   :   "DO NOT"   fifty   :   "DO NOT"   51   :   "DO NOT"   52   :   "DO NOT"   53   :   "DO NOT"   54   :   "DO NOT"   55   :   "DO NOT"   56   :   "DO NOT"   57   :   "DO NOT"   e_cancela_biometria   :   "634"   e_cancela_boletaf   :   "65"   e_cancela_registro   :   "456"   e_compraventa   :   "0"   e_desembargo   :   "0"   e_deuda_acreedor_hipotecario   :   "0"   e_deuda_acreedor_personal   :   "0"   e_domicilios   :   "0"   e_favor_boletaf   :   "eleven"   e_favor_registro   :   "one"   e_honoraries   :   "0"   e_impuestos   :   "612"   e_levamiento_aceptacion   :   "0"   e_other   :   "0"   e_pazysalvo_areametropolitana   :   "0"   e_pazysalvo_predial   :   "46"   e_pazysalvo_valorizacion   :   "146"   e_remanent   :   "0"   egreso_lazo_oriente   :   "56565"   id   :   "86"   j_cancela_biometria   :   "DO NOT"   j_cancela_boletaf   :   "DO NOT"   j_cancela_registro   :   "DO NOT"   j_compraventa   :   "DO NOT"   j_desembargo   :   "DO NOT"   j_deuda_acreedor_hipotecario   :   "DO NOT"   j_deuda_acreedor_personal   :   "DO NOT"   j_domicilios   :   "DO NOT"   j_favor_boletaf   :   "DO NOT"   j_favor_registro   :   "DO NOT"   j_honoraries   :   "DO NOT"   j_ taxes   :   "DO NOT"   j_leading_acceptance   :   "DO NOT"   j_other   :   "DO NOT"   j_pazysalvo_areametropolitana   :   "DO NOT"   j_pazysalvo_predial   :   "DO NOT"   j_pazysalvo_valorizacion   :   "DO NOT"   j_remanent   :   "DO NOT"   client name   :   "JAIRO MARTINEZ"   p_cancela_biometria   :   "5"   p_cancela_boletaf   :   "3614"   p_cancela_registro   :   "6"   p_compraventa   :   "0"   p_desembargo   :   "0"   p_deuda_acreedor_hipotecario   :   "0"   p_deuda_acreedor_personal   :   "0"   p_domicilios   :   "0"   p_favor_boletaf   :   "one"   p_favor_registro   :   "one"   p_honoraries   :   "0"   p_posts   :   "6345"   p_lead_acceptance   :   "0"   p_other   :   "0"   p_pazysalvo_areametropolitana   :   "64"   p_pazysalvo_predial   :   "16"   p_pazysalvo_valorizacion   :   "6"   p_remanent   :   "0"   valuereceived   :   "332"

    
asked by Gabriel Uribe Gomez 04.04.2018 в 02:08
source

1 answer

2

Well the undefined is because in var nombre=respuesta[1]; the position one does not exist if not the 0 , which is where the first data is, if you want to get the name of the serious client, var nombre=respuesta[0][1];

    
answered by 04.04.2018 / 02:58
source