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