when entering code fill in automatic description

0

What kind of friends do I have a php from which registers the products in a table but in another table I have loaded the data of the products as they are barcode and name that in the from when entering the barcode it automatically extracts the name or description of related to said code and upload it in an input text so that it is then sent via the from to the database

form code

<html>
    <head>
    <title>Ingreso productos</title>
   </head>
    
    
<body>

<center>
     <p><h1>Registro de productos </h1></p>
    <form action="operacion_guardar.php" method="POST"><br/><br/><br/>  
    
	<input type="text"  REQUIRED name="codbar" placeholder="codigo barras" value="" /><br/><br/>
	<input type="text"  REQUIRED name="descripcion" placeholder="Descripcion" value="" /><br/><br/>
	<input type="number"  REQUIRED name="cantidad" placeholder="cantidad" value="" /><br/><br/>
	<input type="date"  REQUIRED name="fecha_v" placeholder="Fecha de vencimiento" value="" /><br/><br/>
        
    <input type="submit" value="Registrar"/>
        
    
    </from>
  </center>  
    
    
    </body>
</html>

    
asked by arthur k 02.04.2018 в 00:33
source

1 answer

0

It would be nice if you gave your code to be able to help you better.

According to the problem you are raising, I think that if you pick up the 'onchange' or 'keypress' event from the bar code input you can send an AJAX request to your php server.

In the PHP file you can consult the barcode data and return the answer to your query.

And in the success of the ajax because you already insert the answer of your php in the description and quantity inputs.

Then I write a pseudo code to understand the idea, but if you do not provide your code something I can not be more specific.

Html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .check {
            background-color: red;
            font-size: 2em;
            border: 1px solid black;
            padding: 10px;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
    function checkData() {
        var codeBar = $('#codeBar').val();
        $.ajax({
            url: "test.php",
            data: {codeBar: codeBar}
            success: function (result) {

               $('#description').val(result.description);
               $('#date').val(result.fecha);

            }
        });
    }
</script>

<input id="codeBar" onchange="checkData()">
<input id="description">
<input id="date">
</body>
</html>

PHP test.php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

try {
     $codeBar=$_GET["codeBar"];
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $stmt = $conn->prepare("SELECT description , cantidad , fecha  FROM product WHERE codeBar=$codeBar");
     $stmt->execute();


    $result = $stmt->fetchAll();
    $result = json_encode($result);

    echo $result;
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
    
answered by 02.04.2018 в 01:06