Select field fill according to code entered in another field

Asked

Viewed 924 times

0

Based on the image below, I would like when the person swiped their card, a select was made in mysql and where the store is written to appear the store where the person is registered.

How could I sue ?

The bigger question is, how to select using the above field:(Operator)

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

Follows current code:

<?php 
 include("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>

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 picking it up with AJAX.. For example, 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 more or less 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 take-shop.php gets 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 did here in the nail, if you have any questions, ask there .. I hope to have helped.

  • Hi, sorry for the delay,I tried here and it did not work,possibly I got it wrong,posted as the code in the question.

  • I got it right with your tip,.

Browser other questions tagged

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