autocomplete bring information to 2 different fields

Asked

Viewed 326 times

0

The problem I am going through is due to the fact that I need to return 2 different values for different fields, from an autocomplete that is working in the field "product".

It searches via sql and returns the product name already, now I want the value of the quantity column in the database also fill the stock field in the table.

I’m lost in it, if anyone can help me thank you !!

HTML

`<input type="text" name="produto" class="produto' + contador + '" />`
`<input type="text" name="estoque' + contador + '" />`

JS

$(".produto").autocomplete({
source: "../../../sistema/PDV/assets/php/busca_produtos.php",
minLength: 2,

      select: function(event, ui){

      //event.preventDefault();

      var1 = ui.item.produto_id +' - '+ ui.item.produto_nome ;

      $('input[name="produto' + contador + '"]').val( var1 ),
      $('input[name="estoque' + contador + '"]').val (ui.item.produto_qtd )

      }

});

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,quantidade FROM produtos WHERE nome LIKE :term ");
    $stmt->execute(array('term' => '%'.$_GET['term'].'%'));

    while($row = $stmt->fetch()) {

        // $return_arr[] =  $row['id'].' - '.$row['nome'].' - '.$row['quantidade'] ;

       $return_arr[] =  array(

           'value' => $row['nome'], //Valor para referência na interface


           'produto_id'    => $row['id'],
           'produto_nome'  => $row['nome'],
           'produto_qtd'   => $row['quantidade']
       );

} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}


echo json_encode($return_arr); 

}

After corrections in the code with the help of @Wilson I was able to display the information but there is a counter on the dynamic grid where I refer each line and item by it. And when add 2 or more lines the result always appears in the last created item, if it is possible to adjust this failure would be of great help ... Thanks in advance !

1 answer

1


Thiago, first we need to change your PHP result to return a JSON object from an array, like this:

PHP

while(...){
    $return_arr[] =  array(
        'value' => $row['nome'], //Valor para referência na interface
        'produto_id' => $row['id'],
        'produto_nome' => $row['nome'],
        'produto_qtd' => $row['quantidade'],
    );
}

echo json_encode($return_arr);

After that, we have to define a callback for the event select, which is the event triggered when the user selects an item from the list. Note that we can access the value sent by php’s JSON by the object ui.item. This way, we can change the value of the parameter value of input, is thus:

JS

$(".produto").autocomplete({
    source: "../../../sistema/PDV/assets/php/busca_produtos.php",
    minLength: 2,
    select: function(event, ui){
        event.preventDefault();
        $('input[name="produto"]').val(ui.item.produto_id),
        $('input[name="estoque"]').val(ui.item.produto_qtd)
    }
});

Reference: https://api.jqueryui.com/autocomplete/#Event-select

  • Even worked the way I need, but it is not displaying the val() in the product field, the search is all blank and when I select remains blank, the stock appeared cool. , which can be done to fix this, because when I give a console.log is displaying the right values !

  • Tested here by adding event.preventDefault(); at the beginning of select’s Function. I edited the answer. In my test it worked here.

  • does not display in the search yet, but when I select returns the value yes. Improved but not this as it has to be, which is hindering the display of the search

  • Tell you what, let’s put the field value in PHP. I think jquery is lost without this field! I edited the answer. PS: if you want to return the value of the search to the product field event.preventDefault();

  • Sensational friend, it worked well too thanks !! If possible now I will ask for a support in sequence for the code to be perfect.

  • Thiago, if you solved your problem, mark the answer as a solution. For another problem I suggest you open another topic in Stack!

  • Okay, thanks young tenhau m great day !!

Show 2 more comments

Browser other questions tagged

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