Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
JavaScript send data from input field to ajax request

I have following Code. But the String in the Database is empty. Why does it not work to get the string from the input field?

HTML

 <div id="todo">
   <h2>Todo Liste</h2> 
   <form method="get">
     <input type="text" name="username" placeholder="name" required>
     <input type="text"  class="inputField" name="inputData" id="myInput" placeholder="title"  required><br><br>
     <span onclick="newElement(); sendData(this.value)" class="addBtn">Add</span><br><br>
    </form>

JavaScript

function sendData(str) {
  const xmlhttp = new XMLHttpRequest();
  xmlhttp.onload = function() {
    document.getElementById("txtHint").innerHTML = this.responseText;
  }
  xmlhttp.open("GET", "saveData.php?q=" + str);
  xmlhttp.send();
}

To get the Value on the html page:

<p>Response from PHP <span id="txtHint"></span></p> 

to bring the data in the html

function newElement() {
var li = document.createElement("li");
var inputValue = document.getElementById("myInput").value;
var t = document.createTextNode(inputValue);
    li.appendChild(t);
  
      document.getElementById("myUL").appendChild(li);
    
  }

and i want to get the data of the text, a user typed in. this is where i fail.

My idea was

console.log(document.getElementsByClassName("inputField"));

How can I get the element?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda