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

134
Visualizações
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 à 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