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

127
Vistas
How can I avoid typeError on a function that sends formdata via Ajax XMLHttpRequest?

I'm trying to send form data calling a function in the form button that sends a XMLHttpRquest via ajax. The problem is that when the data is sent the first time the function works ok, but whenever I fill the form again to send new data without reload the page, the function in the button is not recognized, it throws TypeError and the console says that "myFunction" is not a function. I tried already to reorder javascript but isn't working. Maybe I'm missing something. Can you guys help me?

This is the HTML:

<form autocomplete="off" id="insert_user">
   <input type="text" name="name" placeholder="Name">
   <input type="text" name="lname" placeholder="Surname">
   <input type="text" name="document" placeholder="Document">
   <input type="tel" name="mobile" placeholder="Phone">
   <input type="email" name="email" placeholder="Email">
   <button type="button" onclick="insertUser()">Send</button>
</form>
<div class="row" id="users_row">
   <!-- List of users loaded with Ajax -->
</div>

This is the Javascript:

function loadUsers() { //Allows to show a list of users via ajax.
   xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
         document.getElementById("users_row").innerHTML = this.responseText;
      }
   };
   xhttp.open("GET", "views/users_view.php", true) //This file runs the query that shows the data.
   xhttp.send()
}
function insertUser() { // Allows to insert user data via ajax
   xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
         document.getElementById("saved_user").innerHTML = "<b class='text-primary'>User Added<b>"; 
         loadUsers() // Refresh the list
         setTimeout(function() {
            document.getElementById("saved_user").innerHTML = ""
         }, 3000)
      }
    }
    xhttp.open("POST", "controller/insert_user.php", true) //This file runs the query that inserts data from the form.
    insertUser = document.getElementById("insert_user")
    formData = new FormData(insertUser)
    xhttp.send(formData)
 }

The scripts are placed at the end of the body tag. This is the response in the console when I try to send new information through the form:

Uncaught TypeError: insertUser is not a function at HTMLButtonElement.onclick

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