How do I pass php array to Jquery?

0

I have a little problem:

<?php 
require_once '../Controlador/HuespedController.php';
$Huesped = new HuespedController;
$userdoc = '44889498'; //originalmente aqui va esto $_POST['doc'],
$consulta = $Huesped->BuscarDocumento($userdoc);// se llama a la funcion

print_r($consulta);
?>

This code serves very well the problem is that this query to the bd returns an array that is the following:

  

Array ([0] => Array ([clicod] = > 1 [tpcli] = > Principal2 [country]   = > 51Peru [doctp] = > DNI [doc] = > 44889498 [docexp] = > 2018-07-08 [name] = > Augusto [lastname] = > Pruebita [email] = > [email protected]   [phone] = > 999569041 [address] = > Urb. Villa Flores))

How can I pass this to the jquery, and tried with $ .post (), but giving me a tremendous array is very complicated, is there a more effective way to pass it?.

psdt: e seen that some use Json_encode (), but I do not know how to use it and how to pass it to the jquery.

Thank you very much for taking the time to read my question posted, I thank you very much for sharing my knowledge.

    
asked by Israel Correa Quevedo 30.05.2018 в 23:37
source

1 answer

2

With an answer in json it is easier for you. You can make use of $ .post (). I usually use $ .ajax () for this type of use. If you want to use ajax, you could do it for example:

$.ajax({
    url: 'resultado.php',
    type: 'post',
    dataType: 'json', //faltaba la comita
    error: function () {
        alert('Se presentó un error');
    },
    success: function (data) {
        $.each(data, function (indice, valor) {
            console.log('clicod: ' + valor.clicod + ' - Indice: ' + indice);
            console.log('tpcli: ' + valor.tpcli + ' - Indice: ' + indice);
        })
    }
});

In your result.php, you can ask your controller how you are doing it, only that you have to return the answer with json_encode:

<?php 
require_once '../Controlador/HuespedController.php';
$Huesped = new HuespedController;
$userdoc = '44889498'; //originalmente aqui va esto $_POST['doc'],
$consulta = $Huesped->BuscarDocumento($userdoc);// se llama a la funcion

echo json_encode($consulta);
?>

If you want more info about jQuery ajax: link

If you need to know more about json: link

    
answered by 30.05.2018 / 23:52
source