Echo in real time

0

How can I display what was typed in a <input /> or <textarea> real-time field in PHP, same as Stack-Overflow?

    
asked by anonymous 18.09.2016 / 21:10

1 answer

3

With php you can not do this "in real time". PHP is a server-side language (it handles things on the server side), which will send a data output to the client (browser).

After this output occurs, what can be done is to use Javascript to perform such an operation.

See:

window.onload = function () {
     
  function qs(el){
    return document.querySelector(el);
  }
  
  var saida = qs('#saida');
  
  qs('#texto').addEventListener('input', function () {
      saida.innerHTML = this.value;  
  });
}
<div id="saida"></div>
<textarea id="texto"></textarea>

In this case, I used the input event to capture what is typed in textarea and then pass it to the html. But you can use keyup .

Warning : To avoid injecting HTML into your code, instead of using innerHTML , use innerText .

window.onload = function () {
     
  function qs(el){
    return document.querySelector(el);
  }
  
  var saida = qs('#saida');
  
  qs('#texto').addEventListener('input', function () {
      saida.innerText = this.value;  
  });
}
<div id="saida"></div>
<textarea id="texto"></textarea>
    
18.09.2016 / 21:22