Save input value in real time

0

I have the following input :

<input id="searchinputid" type="text" class="searchinput" name="txtbuscan" placeholder=" Search..." onkeyup="showUser(this.value)" autocomplete="off"></input>

and wanted to save its value in real time when the user gave Enter , that is, I wanted to save the input value in the PHP variable whenever the user gave Enter no input .

$fgdsj="<script>$('#searchinputid').val();</script>";
    
asked by anonymous 11.11.2017 / 20:28

1 answer

0

For you to solve your problem, you can use AJAX. With the help of the jQuery library, we can do the following:

(function ($) {
  'use strict';

  $(function () {
    $(window).on('keydown', function (event) {
      if (event.keyCode !== 13 && !$('#searchinputid').is(':focus')) {
        return;
      }

      $.post('/endereço', {
        param: $('#searchinputid').val()
      })  
        .done(function () {
          console.log('Postado!');
        })
        .fail(function () {
          throw new Error('Erro.');
        });
      ;
    });
  });
}(jQuery));

Simply change /endereço to your PHP page where you will save the input value. Also change the param by the name of the $_POST[] that you are using.

    
11.11.2017 / 21:11