form has a problem retrieving the value of Post

3

I'm doing a site and I wanted it so that the person typed in the link javascript would pick up and send the form but he is sending it but when I try to recover it with $ _POST it n retrieves the value that was sent q code I'm using .

<?php echo $_POST['link'] ; ?>
<form name="form" action="" method="POST" id="meuForm" name="meuForm">
      <input type="txt" name="link" id="link" class="form-control form-control-lg"  placeholder="Link Para Encurtar"  onkeydown="myFunction()">

</form>

<script>
    function myFunction() {
        meuForm.submit(); 
    }
</script>
    
asked by anonymous 14.05.2018 / 01:15

2 answers

2

To work as desired you have to use onblur="myFunction()"

That way the function is called when you click outside of input

  

If you use onkeydown the function is triggered as soon as the key is pressed, so there is no time to pass anything.

Using crt+v or digitando use jquery

library

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Script

$("#link").on("paste change", function () {
   setTimeout(function() {
      $('#meuForm').trigger('submit')
   });
});

PHP

<?php echo $_POST['link'] ; ?>
<form name="form" action="" method="POST" id="meuForm" name="meuForm">
<input type="txt" name="link" id="link" class="form-control form-control-lg"  placeholder="Link Para Encurtar">
</form>
  

NOTE: change will act as if it were blur . You have to type and then click outside the input. Now with crt+v the function fires automatically when you paste the text into the input.

    
14.05.2018 / 01:39
1

To send the form to the same page there is no need to have the attribute action , consequently, <script> tbm does not:

<?php 
    if(isset($_POST)&&!empty($_POST)){
        echo $_POST['link'];
    } 
?>
<form name="form" method="POST" id="meuForm" name="meuForm">
      <input type="txt" name="link" id="link" class="form-control form-control-lg"  placeholder="Link Para Encurtar"  onkeydown="myFunction()">
      <input type="submit" name="enviar" id="enviar" class="btn btn-block btn-lg btn-primary" value="ENCURTAR"> 
</form>
    
14.05.2018 / 01:30