6
Description
Need: I need to make an autocomplete with jQuery
, PHP
and from what was selected, caught the ID
, I make a query in the database and display what I need.
Problem: I’m having trouble displaying the records on input
which loads the autocomplete. Note the code below Javascript
.
Javascript (jQuery)
$( "#consulta_estoque" ).autocomplete({
source: 'js/autocomplete.php',
select: function(event, ui){
$( "#consulta_estoque" ).val(ui.item.descricaoProduto);
alert(ui.item.codigoFabrica);
}
});
PHP
foreach ($sql as $res) {
$resultado[] = $res;
}
echo json_encode($resultado);
JSON
[
{
"codProduto":"9",
"codigoFabrica":"8019077",
"codSubcategoria":"0",
"descricaoProduto":"WWAKS3-5\/S366 CONECTOR M12 EUROFAST ANGULAR FEMEA",
"tipoItemEstoque":"0",
"c7flex":"9",
"ncmProduto":"85444200"
},{
"codProduto":"39",
"codigoFabrica":"8019078",
"codSubcategoria":"0",
"descricaoProduto":"WWAKS3-10\/S366 CONECTOR M12 EUROFAST ANGULAR MACHO",
"tipoItemEstoque":"0",
"c7flex":"39",
"ncmProduto":"0"
}
]
Observing: I believe that the JSON
that is correct.
Can someone help me with that?
From what I see, your JSON is an Array, so it wouldn’t be right to access:
ui.item[0].descricaoProduto
instead ofui.item.descricaoProduto
? I would like you to make available the entire PHP so that I can include it in http://jsfiddle.net to make a functional example– Paulo Roberto Rosa
So friend, the whole php is complicated, because there are many classes to provide
– Daniel Swater
The problem is that I have no way to test... so test the following to see if it works:
$( "#consulta_estoque" ).val(ui.item[0].descricaoProduto);
andalert(ui.item[0].codigoFabrica);
– Paulo Roberto Rosa
Open the browser console (F12) and see if you have any error popping up.
– Paulo Roberto Rosa
PHP seems to be correct because JSON is right. By plugin documentation the use also seems certain.
– Gustavo Rodrigues
Yeah, and if I do like this: $result[] = $res['descricaoProduct']; Shows right, but I can’t get the id that was clicked
– Daniel Swater
If you do as JSON does?
– Gustavo Rodrigues
["WWAKS3-5/S366 EUROFAST ANGULAR FEMEA M12 CONNECTOR","WWAKS3-10/S366 EUROFAST ANGULAR MALE M12 CONNECTOR"]
– Daniel Swater
Thanks for the @Gustavorodrigues documentation link was very helpful to develop the reply.
– Paulo Roberto Rosa
None of the answers answers your question?
– durtto