D
The problem you have is that http://api.jqueryui.com/autocomplete/ Wait a json with the fields label and value. sourceType: Array or String or Function( Object request, Function
response( Object data ) Default: none; must be specified Defines the
data to use, must be specified.Independent of the variant you use, the label is always treated as
text. If you want the label to be treated as html you can use Scott.
González' html extension. The demos all focus on different variations of the source option - look for one that matches your use case, and check out the code. Multiple types supported:Array:
An array can be used for local data. There are two supported formats:An array of strings: [ "Choice1", "Choice2" ]An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]The label property is displayed in the suggestion menu. The value will be inserted into the input element when a user selects an item.
If just one property is specified, it will be used for both, e.g., if
you provide only value properties, the value will also be used as the
label....(continued)Source: http://api.jqueryui.com/autocomplete/ Either you generate the json that way or you use a function to convert it.Instead of putting: response(data);
You'd use: response($.map(data, function (item) {
return {
label: item.nombre,
value: item.id
}
}));
The function of the autocomplete would remain:$("#responsables").autocomplete({
source:function(request,response){
$.ajax({
url: "https://api.myjson.com/bins/1fcz36",
type:"GET",
dataType:"json",
data:{
search: request.term
},
success:function(data){
response($.map(data, function (item) {
return {
label: item.nombre,
value: item.id
}
}))
}
})
}
})
The json data: http://myjson.com/1fcz36 The executable code: https://jsfiddle.net/k3an9uo4/1/ (bell)The shown so far was a static example of how to configure the http://api.jqueryui.com/autocomplete/ from jQuery UI according to the original question.To respond dynamically, you have to configure the ajax to run a script and this will return the data as typed by the user.How we can check in the documentation http://api.jquery.com/jquery.ajax/ in the parameter url indicate the route to the script, in type configure the method to use (GET or POST) and data the parameters we want to pass to the script, in our case, at least the text typed by the user. $("#responsables").autocomplete({
source:function(request,response){
$.ajax({
url: "consulta_ajax.php",
type:"GET",
dataType:"json",
data:{
search: request.term
},
success:function(data){
response($data)
}
})
}
})
The most comfortable, if we have the control of the data source, is that the
own script return the json with the fields label and value (
alone. label if we do not want to use indices) to not complicate
javascript.consultation_ajax.php<?php
$mysqli = new mysqli(DBURI,DBUSER,DBPASS,DBNAME);
$texto = $_GET['search'];
$sql = " SELECT id AS value, nombre AS label FROM ciudades WHERE nombre LIKE '%$texto%' ";
$result = $mysqli->query($conexion, $sql);
while ($myrow = $result->fetch_array(MYSQLI_ASSOC)) {
$resultado[]=$myrow;
}
echo json_encode($resultado);
?>