Add products automatically by means of bar code scanner input

0

I have a problem, what I need is the following, I have an input where I would like to put the corresponding product code automatically to the table below, as shown in the image, as Could I achieve that? Assuming that it is the barcode of the product that should be sent to call.

    
asked by WilsonicX 16.10.2018 в 22:38
source

1 answer

1

This is one of the most simple ways I find that you could do what you need

 $("#inputCode").keyup(function(){ 
var codigo=$("#codigo").val()//obtenermos el codigo escrito en el input

   if(codigo.length > 5){//si el valor de el codigo introducido es mayor a 5 ejecuta el ajax

    $.ajax({
                    url: 'traer_codigo.php',
                    type: 'POST',
                    data: { codigo: codigo},
                    dataType: "json",
                    cache: false,
                    contentType: "application/json",
                     success: function (data) {                 
                         //execute call back.
                         //hacemos una nueva fila en la tabla eh introducimos los valores del call back en sus repestivos campos  
                         $('#mytablel').append('
                            <tr>
                            <td>data["codigo"]</td>
                            <td>data["nombre"]</td>
                            <td>data["marca"]</td>
                            <td>data["precio"]</td>
                            <td>data["cantidad"]</td>
                            <td>data["importe"]</td>
                          </tr>');

                    },
                    error: function (xhr, status) {

                    },
                });
          });
}

bring_code.php

<?php
  $codigo=$_POST["codigo"];//recivimos desde ajax el valor del input
//en esta consulta estoy asumiendo que tu coneccion a la base de datos se llama conn
 $sql=mysqli_query($conn,"SELECT * FROM codigos WHERE codigo='$codigo'");

 $row=mysqli_fetch_array($sql);

//mandamos la informacion obtenidad de la consulta en un array multidimencional
 echo json_encode(array
                 ("codigo"=>$row["codigo"]
                 ,"nombre"=>$row["nombre"]
                 ,"marca"=>$row["marca"]
                 ,"precio"=>$row["precio"]
                 ,"cantidad"=>$row["cantidad"]
                 ,"importe"=>$row["importe"]
               ,))

?>

I hope you greetings

    
answered by 16.10.2018 / 23:15
source