receive two autocomplete values and pass to < input type='Hidden' >

Asked

Viewed 167 times

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({&#xA; source: "../../../centrolimp/assets/php/busca_produtos.php",&#xA; minLength: 2,&#xA; select: function( event, ui ) {&#xA; "Selecionado: " + ui.item.value + ", Id: " + ui.item.id :&#xA; }&#xA; });

  • Not receiving the value ... via console.log(ui.item.value);

  • See https://answall.com/questions/258268

  • 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?

  • 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.

  • and this generates an error tbm ... Uncaught Error: no such method 'instance' for instance widget

  • Ready, edited .... I had forgotten to put this part !!

  • Just do as @Guilhermecostamilam said, change $return_arr[] = $row['id'].' - '.$row['nome'] ; for $return_arr[] = array("id" => $row['id'], "value" => $row['nome']);

  • 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 ??

  • The problem to make it work has to explain .... was giving incopatibility to the version of jquery ui

  • Have you solved ? How is the structure HTML ? The fields it says, is the search and the id?

  • not yet, insert the part of the code that inserted the rows in the table dynamically.

  • hi friends, you tried to return the two values ?? still to Arrado in that .

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.