Select field to fill according to code entered in another field

0

Based on the image below, I would like that when the person passes the card of her, was made a select in mysql and where it is written the store appeared the store in which the person is registered.  

How could I process?

The biggest doubt is, how to do the select using the field above: (Operator)

That is, it would be a dynamic select, which would change according to the employee's card number, and even more, if he did not have the card he would appear as for example "Employee without registered store."

Followthecurrentcode:

<?phpinclude("conexao.php");

?>

<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
<head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
        <title></title>
  <link rel="stylesheet" href="includes/css/style.css">
  <link rel="stylesheet" href="includes/css/bootstrap.min.css">
  <link rel="stylesheet" href="includes/css/datatables.css">
  <script src="includes/js/jquery.min.js"></script>
  <script src="includes/js/bootstrap.min.js"></script>
  <script src="includes/js/jquery.dataTables.min.js"></script>
  <script src="includes/js/datatables.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    // quando for alterado
    $("form").on("change", "#operador_logica", function() {
        $.ajax({url: "pega_loja.php?operador="+$("#operador_logica").val(), success: function(resultado) {
            // faz a lógica
            var json = $.parseJSON(resultado);
            $("#loja_logica").val(json.lojaNome);
        });
    }
}
</script>  
    </head>
<body>
    <div class="container">
        <div class="row">
            <div class="span12" style="text-align:center; margin: 0 auto;">
                <form class="form-horizontal" style="width: 600px; margin: 0 auto;" method="POST" action="imprimir.php">
                    <fieldset>
                         <legend>CONTROLE DE VASILHAMES</legend>
                         <div class="form-group">
                           <label for="operador" class="col-lg-2 control-label">OPERADOR(A):</label>
                           <div class="col-lg-10">
                             <input type="text" class="form-control" id="operador" name="operador" placeholder="PASSE SEU CARTÃO ...">
                           </div>
                         </div>
                         <div class="form-group">
                           <label for="select" class="col-lg-2 control-label">LOJA:</label>
                           <div class="col-lg-10">
                            <?php
                             $operador = $_GET["operador"];
                              $q = mysql_query("SELECT * FROM usuarios WHERE usu_cod = '".$operador."' LIMIT 1");
                              if(mysql_num_rows($q)) {
                                  $a = mysql_fetch_assoc($q);
                                  $retorno["usu_num_loja"] = $a["usu_num_loja"];
                              } else {
                                  $retorno["usu_num_loja"] = false;
                              }

                              header("Content-Type: application/json");
                              echo json_encode($retorno);
                            ?>
                            Loja: <input type="text" name="loja" id="loja_logica" /><br />

                            </div>
                          </div>

                         <div class="form-group">
                           <label for="select" class="col-lg-2 control-label">VASILHAME:</label>
                           <div class="col-lg-10">
                           <select type="text" class="form-control" name="vasilhame" id="vasilhame">
                                <option selected value=''></option>
                                <?php  
                                $consulta_loja=mysql_query("SELECT * FROM vasilhame ORDER BY vas_id ASC"); 
                                while ($dados = mysql_fetch_array($consulta_loja)) {
                                  echo("<option value='".$dados['vas_id']."'> ".$dados['vas_desc']."   </option>");}
                                   ?>
                              </select>
                            </div>
                          </div>
                        <div class="form-group">
                           <label for="quantidade" class="col-lg-2 control-label">QUANTIDADE:</label>
                           <div class="col-lg-10">
                             <input type="text" class="form-control" id="quantidade" name="quantidade" placeholder="DIGITE A QUANTIDADE ...">
                           </div>
                         </div>
                         <div class="form-group">
                           <div class="col-lg-10 col-lg-offset-2">
                             <button type="submit" class="btn btn-danger">GERAR CUPOM</button>
                           </div>
                         </div>
                    </fieldset>
                </form>
    </div>
  </div>
</div>

</body>
</html>
    
asked by anonymous 29.04.2016 / 00:43

1 answer

1

Do you use Javascript? You can do this with AJAX. The jQuery library has the well-defined and easy-to-use method .. You can have a PHP file returning a json and get it with AJAX .. For ex, you have this form:

<form>
Operador: <input type="text" name="operador" id="operador_logica" /><br />
Loja: <input type="text" name="loja" id="loja_logica" /><br />
<!-- continuação do código ... -->
</form>

And after jQuery instantiated in the code, you do something like this:

<script type="text/javascript">
$(document).ready(function() {
    // quando for alterado
    $("form").on("change", "#operador_logica", function() {
        $.ajax({url: "pega_loja.php?operador="+$("#operador_logica").val(), success: function(resultado) {
            // faz a lógica
            var json = $.parseJSON(resultado);
            $("#loja_logica").val(json.lojaNome);
        });
    }
}
</script>

And your PHP code pega_loja.php looks like this:

<?php

// continuação do código de conexão ..

$operador = $_GET["operador"];
$q = mysql_query("SELECT * FROM tabela_lojas WHERE operador = '".$operador."' LIMIT 1");
if(mysql_num_rows($q)) {
    $a = mysql_fetch_assoc($q);
    $retorno["lojaNome"] = $a["lojaNome"];
} else {
    $retorno["lojaNome"] = false;
}

header("Content-Type: application/json");
echo json_encode($retorno);

With the idea of the image you passed, this should solve .. I made it here on the nail, if you have any questions, ask there .. I hope I have helped.

    
29.04.2016 / 01:45