1
I’m using Jquery, Ajax, PHP and MYSQL to do Autocomplete in a Brazilian city search field.
When I type the name of the city the suggestions appear and when I pass the mouse over each city the code automatically fill in the form with the name of the country, state, city, latitude, longitude and Timezone for the selected city, until then everything works.
The problem is: When the search returns cities with the same name, when I select one of these of the same name, only the data of the first of the list are filled in the form, no matter which one I select, only the data of the first one are filled, as shown in the example below:
When I select Cedral (Maranhão) it fills with Cedral (São Paulo).
I would like to know what changes I need to make to the code in order for it to fill out the form correctly, with the respective data of the city I select. NOTE: Database rows have unique id’s (id column).
Follow the code below:
File: custom.js
$(function() {
// Atribui evento e função para limpeza dos campos
$('#busca').on('input', limpaCampos);
// Dispara o Autocomplete a partir do segundo caracter
$( "#busca" ).autocomplete({
minLength: 2,
source: function( request, response ) {
$.ajax({
url: "consulta.php",
dataType: "json",
data: {
acao: 'autocomplete',
parametro: $('#busca').val()
},
success: function(data) {
response(data);
}
});
},
focus: function( event, ui ) {
$("#busca").val( ui.item.cidade );
carregarDados();
return false;
},
select: function( event, ui ) {
$("#busca").val( ui.item.cidade );
return false;
}
})
.autocomplete( "instance" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( item.pais + " - " + item.cidade + " (" + item.estado + ") " )
.appendTo( ul );
};
// Função para carregar os dados da consulta nos respectivos campos
function carregarDados(){
var busca = $('#busca').val();
if(busca != "" && busca.length >= 2){
$.ajax({
url: "consulta.php",
dataType: "json",
data: {
acao: 'consulta',
parametro: $('#busca').val()
},
success: function( data ) {
$('#pais').val(data[0].pais);
$('#cidade').val(data[0].cidade);
$('#estado').val(data[0].estado);
$('#latitude').val(data[0].latitude);
$('#longitude').val(data[0].longitude);
$('#timezone').val(data[0].timezone);
}
});
}
}
// Função para limpar os campos caso a busca esteja vazia
function limpaCampos(){
var busca = $('#busca').val();
if(busca == ""){
$('#pais').val('');
$('#cidade').val('')
$('#estado').val('');
$('#latitude').val('');
$('#longitude').val('');
$('#timezone').val('')
}
}
});
File: query.php
<?php
// Dados da conexão com o banco de dados
define('SERVER', 'SERVER');
define('DBNAME', 'DBNAME');
define('USER', 'USER');
define('PASSWORD', 'PASS');
// Recebe os parâmetros enviados via GET
$acao = (isset($_GET['acao'])) ? $_GET['acao'] : '';
$parametro = (isset($_GET['parametro'])) ? $_GET['parametro'] : '';
// Configura uma conexão com o banco de dados
$opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
$conexao = new PDO("mysql:host=".SERVER."; dbname=".DBNAME, USER, PASSWORD, $opcoes);
// Verifica se foi solicitado uma consulta para o autocomplete
if($acao == 'autocomplete'):
$where = (!empty($parametro)) ? 'WHERE cidade LIKE ?' : '';
$sql = "SELECT cidade, estado, pais FROM tabela " . $where;
$stm = $conexao->prepare($sql);
$stm->bindValue(1, '%'.$parametro.'%');
$stm->execute();
$dados = $stm->fetchAll(PDO::FETCH_OBJ);
$json = json_encode($dados);
echo $json;
endif;
// Verifica se foi solicitado uma consulta para preencher os campos do formulário
if($acao == 'consulta'):
$sql = "SELECT cidade, estado, pais, latitude, longitude, timezone FROM tabela ";
$sql .= "WHERE cidade LIKE ? LIMIT 1";
$stm = $conexao->prepare($sql);
$stm->bindValue(1, $parametro.'%');
$stm->execute();
$dados = $stm->fetchAll(PDO::FETCH_OBJ);
$json = json_encode($dados);
echo $json;
endif;
It would be commendable if you put html to anyone who wants to test not having the trouble of creating it.
– user60252
Why don’t you look for the
ID
of the city instead of the name and adjust its query?– Marconi
The problem is that you store the name of the city in
#busca
and in functioncarregarDados
, With AJAX, you search for the city data with that name. In PHP, you select from the database the city by name and set a limit of 1 record. This will always return the first record, as it is happening. You must search the city byid
, which is unique to every record.– Woss
Have you checked this zero position, is that not it? date[0]. parents, here you are getting the first on the list. Wouldn’t it be the case to take the key of the selected? by the way, change the return variable date to Sponse or result to not conflict with the submission
– Ivan Ferrer