Talk to the people, all good! I have a webserver application that does not have a webserver. I have a webserver application that I have a webserver on. I have a webserver application that has a webservice. are correct for the source to handle the information, but autocomplete does not associate what I'm searching for and displays the entire list of results. I tried to create a function to query the webservice and store the result in a variable, so I only use var inside the source, but it did not work to pass an external function inside the source. follow me code to see if someone gives a help!
$(".j_destino").autocomplete({
minLength: 2,
source: function (request, response) {
$.ajax({
url: "_cdn/ajax/destino.php",
dataType: "json",
type: 'POST',
data: {action: 'searchCity'},
success: function (resposta) {
response(resposta.dados);
console.log(response);
}
});
},
focus: function (event, ui) {
$(".j_destino").val(ui.item.city);
return false;
},
select: function (event, ui) {
$(".j_destino").val(ui.item.city);
$(".j_CityId").val(ui.item.id);
return false;
}
}).autocomplete("instance")._renderItem = function (ul, item) {
return $("<li>").append("<a>" + item.city + "</a>").appendTo(ul);
};
This would be the alternative version and more optimized to do this however I'm not hitting something.
function carregaDados() {
$.ajax({
url: "_cdn/ajax/destino.php",
dataType: "json",
type: 'POST',
data: {action: 'searchCity'},
success: function (resposta) {
response(resposta.dados);
}
});
}
$(".j_destino").autocomplete({
minLength: 3,
source: carregaDados(),
focus: function (event, ui) {
$(".j_destino").val(ui.item.city);
return false;
},
select: function (event, ui) {
$(".j_destino").val(ui.item.city);
$(".j_CityId").val(ui.item.id);
return false;
}
}).autocomplete("instance")._renderItem = function (ul, item) {
return $("<li>").append("<a>" + item.city + "</a>").appendTo(ul);
};
This would be my PHP
<?php
session_start();
$getPost = filter_input_array(INPUT_POST, FILTER_DEFAULT);
$setPost = array_map('strip_tags', $getPost);
$post = array_map('trim', $setPost);
$Action = $post['action'];
$jSon = [];
unset($post['action']);
//sleep(1);
if ($Action):
require('../../_app/Config.inc.php');
$jSon['dados'] = null;
endif;
switch ($Action):
case 'searchCity':
$cidades = new pegarCidade;
$cidades->getListaDeCidades();
if ($cidades->getResult()):
foreach ($cidades->getResult() as $key):
$jSon['dados'][] = ['id' => $key->CityId, 'city' => $key->CityNamePT];
endforeach;
endif;
break;
default:
$jSon['error'] = "Erro ao selecionar a ação!";
endswitch;
echo json_encode($jSon);