When Typing in Input load another page without needing the Submit boot

Asked

Viewed 303 times

2

I have a page where it has an input that loads barcode data when opening an application on mobile.

When scanning the product barcode and loading the code into the input, I would like to have the page redirect to the search page of that scanned code without having to click the send button (Ubmit).

Today my page already works with the send button, but I have to scan the product through the camera of the phone and then press the send button, I wanted to automate this task by loading the code already made this send automatically. Follows the code:

<form action="consultar-item.php" method="POST">
       <div class="form-group form-control-lg col-md-4">
        <label for="cod">Busque o Codigo de Barras:</label>

        (<a href="http://zxing.appspot.com/scan?ret=http://localhost:8084/appteste/index.php?codigo={CODE}">Leitor</a>):

        <input type="text" class="form-control" name="cod" value="<?= $_GET['codigo'] ?>"   />
        </div>
        <div class="clearfix"></div>

        <div class="form-group form-control-lg col-md-3">
        <label for="date1">Data Inicial da Pesquisa:</label>
        <input type="date" class="form-control" name="date1" />
        </div>
        <div class="clearfix"></div>

        <div class="form-group form-control-lg col-md-3">
        <label for="date2">Data Final da Pesquisa:</label>
        <input type="date"  class="form-control" name="date2" />
        </div>
        <div class="clearfix"></div>
        <div class="form-group form-control-lg col-md-2">
        <input type="submit"  class="btn btn-success" name="busca" id="butContato" value="ENVIAR" 
        </div>
    </form>
  • Wouldn’t it be possible to do an if/Else with a setTimeout in and with a function that does Submit if the value received in the input is valid? Maybe you don’t even need the setTimeout... or you can do a setInterval to keep checking, and if it’s true you do the Submit. Here’s something that can help you https://www.formget.com/javascript-auto-submit-form/

  • I’ll take a look and then I’ll answer, Thank you!

  • At the event onchange input cannot do Ubmit via js?

1 answer

2

I don’t have full mastery of JS, but as I commented you may have a setInterval that keeps checking the input which receives the code, and if the value of that input is different from 0, (in the rule I made) it sends the form, but you can do there what you want to validate the field and so send or not.

inserir a descrição da imagem aqui

Dry the code from the image above.

let inp = document.querySelector('input');
let form = document.querySelector('form');

let con = '';

function enviar() {
  if(inp.value != con){
    document.body.style.background = "red";
    console.log('leu o código de barra');
    
    // form.submit(); Descomente isso caso queira que o form seja enviado
  } 
}

setInterval(enviar, 200);
<form method="post" action="envia.php">
  <input type="text" id="" name="">      
  <button type="submit">Enviar</button>
</form>

Browser other questions tagged

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