formulario this with problem when recovering the Post value

Asked

Viewed 108 times

2

hello I’m making a website and I wanted so the person type the link javascript pick up and send the form but it is sending but when I try to recover with the $_POST it n recovers the value q was sent code q I am 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>
  • but will send to the same page?

  • yes so I left the action empty, it works like I put action="#" it sends to the same page , and so has the <?php echo $_POST['link']; ? > to q when send it receive and show the value q is in the input

  • removes the action="", lets without and can delete the script q will work

2 answers

2


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

That way the function is called when clicking outside the input

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>

OBS: change it’ll work like it’s blur. You have to type and then click out of the input. Already with crt+v function automatically triggers when pasting text into input.

  • thought to use the onblur but unfortunately n meets my requirement

  • @romulohenrique, which requirements?

  • example if the person has just clicked crt+v to color the text so it sends

  • or when she typed A there she sent the letter A straight without the person needing to click off the input

  • @romulohenrique, then what is the input for ENCURTAR ?

  • with already explained in one of the comments was wrong from the ancient code I even edited and I will say from the,

Show 1 more comment

1

To send the form to the same page there is no need to have the attribute action, consequently, the <script> tbm 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>
  • 1

    i used the script for which the fomulario is sent altomatically then it will be nescesario

  • 1

    the q button was unintentionally copying the old code there still had the button

  • What do you mean? I don’t understand, you want it to be sent dynamically?

  • 1

    yes, automatically so I put javascript with " myForm.Submit(); "to make it so that the function is called

  • https://answall.com/questions/175896/retornar-sa%C3%Addas-do-php-na-mesma-p%C3%A1gina,

Browser other questions tagged

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