Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda