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 !
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 !
– Thiago Lopez
Tested here by adding
event.preventDefault();
at the beginning of select’s Function. I edited the answer. In my test it worked here.– Wilson Faustino
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
– Thiago Lopez
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 fieldevent.preventDefault();
– Wilson Faustino
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 Lopez
Thiago, if you solved your problem, mark the answer as a solution. For another problem I suggest you open another topic in Stack!
– Wilson Faustino
Okay, thanks young tenhau m great day !!
– Thiago Lopez