Returns me undefined

0

Good, I have a form that through AJAX and PHP I make a query to my BD to return some of the data, the problem is that it returns undefined when I try to paint in query a data of the JSON array, I have looked at the debug of the browser and in PHP if you make the query well by returning the array correctly. Where can the fault be? Thanks !!

PS: there are no failures in jquery libraries, etc, etc. They work correctly.

form.php (AJAX)

...                   
                    <form>
                        <div class="form-group">
                            <div class="row">
                                <label class="col-md-3" for="telf">Nº de teléfono:</label>
                                <div class="col-md-9">
                                    <input type="text" name="telf" id="telf" class="form-control">
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6 col-md-offset-4">
                                    <button id="buscar" name="buscar">Buscar</button>
                                </div>
                            </div>
                        </div>
                    </form>

...

  <script>
    $(document).ready(function() {   
        $(document).on("click", "#buscar", function (e) {
            e.preventDefault();
            var telf = $('#telf').val();

    var url = "buscar.php";
    var dataValue = {'telf': telf};

    $.ajax({
        type: "POST",
        url: url,
        data: dataValue,
        cache: false,
        dataType: "json",
        success: function (data) {
            console.log(data.nombre_cli);
        }
    });
    return false;
});
});
</script>

search.php (SQL)

<?php 

require("conexion.php");

$telf= $mysqli->real_escape_string($_REQUEST['telf']);

$stmt = $mysqli->prepare("SELECT nombre_cli, ... FROM ... WHERE num_telefono = ?");

$stmt->bind_param("i", $telf);
$stmt->execute();

$result = $stmt->get_result();

$json = array();
while ($data = $result->fetch_assoc()) {
    $json["data"][] = $data;
}
echo json_encode($json);

$stmt->free_result();
$stmt->close();
    
asked by Cifu 05.05.2017 в 09:27
source

1 answer

2

Returns undefined because you are trying to access a data that does not exist. You are saving your data in PHP within Array with key data so what you will receive will be an object with this form:

{"data": [...datos que insertas desde la base de datos...]}

Or change your implementation of PHP so that you do not create that index data , or you should first access that parameter before trying to access% data% in%:

console.log(data.data<datos a los que quieres acceder>);

On the other hand when calling JavaScript you are creating a fetch_assoc associative so the structure will be:

{data: [{"nombre_cli" : "contenido de nombre_cli"}, ...]}

See the following snippet to help you understand the structure you're creating.

With this structure the way in Array to access the first data would be:

data.data[0].nombre_cli;

It is best to do JavaScript of what you receive from the server:

console.log(data);

This way you will see the structure that has the object and you will be able to access its data guiding you through this structure.

    
answered by 05.05.2017 / 09:48
source