Please pay attention to this code.

function login(){ var xhr = new XMLHttpRequest(); xhr.open("POST", "login.php"); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); var values; values = "email=" + document.getElementById("logemail").value; values = values + "&password=" + document.getElementById("logpassword").value; xhr.send(values); xhr.onreadystatechange = function () { xhr.onreadystatechange = function () { if (this.readyState === 4 && this.status === 200) { if (this.responseText == 'success'){ window.location.href = "forme.local/user.php" } else { document.getElementsById('logerror').innerHTML = this.responseText; } } } } 

}

In addition to this function, there are many others in this js file and they all work, and all also deal with ajax html requests:

 <div id="login"> <p id="logerror" style="display:none;"></p> <form action="javascript:void(null);"> <p class="errors conferr"></p> <input id="logemail" name="logemail" type="text" placeholder="Email"> <input id="logpassword" name="logpassword" type="password" placeholder="Пароль"> <input class="regbutton" type="submit" onclick="login();" name="login" value="Войти"> </form> </div> 

HELP !!! What is the problem?

  • When you try to press the button, it gives: Uncaught TypeError: login is not a function at HTMLInputElement.onclick - Dr Amx
  • changed the name of the function to loginn and everything works, but why doesn’t it work with login? - Dr Amx

1 answer 1

but it doesn't work with login?

Something you have overlaps the login ID in the global scope. Write

 onclick="console.log(login);" 

and find out what it is.

Do not you think that one of them:

 xhr.onreadystatechange = function () { xhr.onreadystatechange = function () { 

superfluous?