Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!