How can I automatically send my input information via javascript?

Asked

Viewed 529 times

3

How can I send information from mine <input /> automatically? Wanted this automatic process to be controlled by the field size of the input. For example, when the value reached 8 characters, it automatically send and move to another page. In this case mine action="sucesso.php". I know this is possible using javascript but I don’t know much about the language.

My code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<?php include 'func.php' ?>
<link href="index.css" rel="stylesheet"></link>
<link href="../Login/css/hover.css"></link>

</head>
<body>

<div class="container">
<form method="Post" action="sucesso.php" name="form">

 <div class="txtbox-container hvr-glow">
   <input type="text" name="txtbox" placeholder="Número do Cartão" autofocus>
 </div>

</form>
</div>
<p><label id="result"></label></p>
<script type="text/javascript">
document.form.submit()

</script>

</body>
</html>

I already have the script in the code that sends automatically but does not have this field size control done.

2 answers

2

You can use onKeyUp to check how many digits the client has already typed and after 8 digits, submit the form.

Would look like this:

<input id="edValue" type="text" onKeyUp="ValueKeyPress()">
    <script>
    function ValueKeyPress(){
       var edValue = document.getElementById("edValue").value;
       if(edValue.length >= 8){
            //submita o form.
       }     
    }
    <script>

Example: https://jsfiddle.net/5v4xjj1a/1/

  • Very good, I think the function onkeyup is the most suitable for the case! + 1

0

The size of the value of the input through the event onchange of it. See:

<input type="text" id="campo1" onkeypress="campo1_change(this);" />

<script>
function campo1_change(obj)
{
    if(obj.value.length < 8) return;
    document.forms[0].submit();
}   
</script>
  • Thiago, the only problem of using onchange is that the function will be called only if the user leaves the field, while he writes, the function is not called. I think the best way would be using Jquery keypress.

  • um, truth. I’ll trade for onkeypress, must solve.

Browser other questions tagged

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