1
how do I pass on a value 'ID - valor'
of a autocomplete jquey
and show only the 'valor'
in the field input
and pass the 'ID'
to another input
guy hidden
.
PHP
if (isset($_GET['term'])){
$return_arr = array();
try {
$conn = new PDO("mysql:host=".DB_SERVER.";dbname=".DB_NAME, DB_USER, DB_PASSWORD);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id,nome FROM produtos WHERE nome LIKE :term ");
$stmt->execute(array('term' => '%'.$_GET['term'].'%'));
while($row = $stmt->fetch()) {
$return_arr[] = $row['id'].' - '.$row['nome'] ;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo json_encode($return_arr);
}
JS
$(".produto").autocomplete({
source: "../../../centrolimp/assets/php/busca_produtos.php",
minLength: 2
});
// Essa parte que insiro as linhas na tabela ...
contador++;
var newRow = $("<tr>");
var cols = "";
cols += '<td class="contador valor_total" >' + contador + '</td>';
cols += '<td><label text-align="center"><input type="text" name="produto' + contador + '" class="produto" /></label></td>';
cols += '<td><label text-align="center"><input type="text" name="qtd' + contador + '" class="qtd spinner" onkeyup="somenteNumeros(this);" /></label></td>';
cols += '<td><label text-align="center"><input type="text" name="preco' + contador + '" class="preco" align="center" /></label></td>';
cols += '<td class="col-md-2 total">R$ 0.00</td>';
cols += '<td><a class="deleteLinha"> Excluir </a></td>';
newRow.append(cols);
I’m already getting the right amount, I just need to split this result now.
In the autocomplete has a property for this.
$( ".produto" ).autocomplete({
 source: "../../../centrolimp/assets/php/busca_produtos.php",
 minLength: 2,
 select: function( event, ui ) {
 "Selecionado: " + ui.item.value + ", Id: " + ui.item.id :
 }
 });
– José Francisco
Not receiving the value ... via console.log(ui.item.value);
– Thiago Lopez
See https://answall.com/questions/258268
– NoobSaibot
If you use that
$return_arr[] = json_encode(array("id" => $row['id'], "valor" => $row['nome']));
or$return_arr[] = array("id" => $row['id'], "valor" => $row['nome']);
returns a json instead of a string?– Costamilam
I’m not getting impletentar @wmsouza ... the idea followed this same .... however I want the 'value' to be in the proper input and the ID in another input to be able to treat it in my form.
– Thiago Lopez
and this generates an error tbm ... Uncaught Error: no such method 'instance' for instance widget
– Thiago Lopez
Ready, edited .... I had forgotten to put this part !!
– Thiago Lopez
Just do as @Guilhermecostamilam said, change
$return_arr[] = $row['id'].' - '.$row['nome'] ;
for$return_arr[] = array("id" => $row['id'], "value" => $row['nome']);
– NoobSaibot
I was able to make it work.... now a big problem is that it is only working on the first input column, because these inputs are dynamic and the auto complete is called by the class ... how to fix this ??
– Thiago Lopez
The problem to make it work has to explain .... was giving incopatibility to the version of jquery ui
– Thiago Lopez
Have you solved ? How is the structure HTML ? The fields it says, is the search and the id?
– NoobSaibot
not yet, insert the part of the code that inserted the rows in the table dynamically.
– Thiago Lopez
hi friends, you tried to return the two values ?? still to Arrado in that .
– Thiago Lopez