I try to make a check for the existence of a login (email) in the database. When checking, I try to make the button Disabled if the login does not exist. When you try to enter a login for verification, the request comes with an answer, but the state of the buttons does not change. And when I try to modify a div (which exists), an error. Chrome writes:

Uncaught TypeError: Cannot read property 'getElementById' of undefinedxmlhttp.onreadystatechange @.

Code:

<script type="text/javascript"> /* Функция, создающая экземпляр XMLHTTP */ function getXmlHttp() { var xmlhttp; try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); } return xmlhttp; } function checkLogin(username) { var xmlhttp = getXmlHttp(); // Создаём объект XMLHTTP xmlhttp.open('POST', 'check_login.php', true); // Открываем асинхронное соединение xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); // Отправляем тип содержимого xmlhttp.send("username=" + encodeURIComponent(username)); // Отправляем POST-запрос xmlhttp.onreadystatechange = function() { // Ждём ответа от сервера if (xmlhttp.readyState == 4) { // Ответ пришёл if(xmlhttp.status == 200) { // Сервер вернул код 200 (что хорошо) if (xmlhttp.responseText) .document.getElementById('login-submit').disabled = false; else document.getElementById("check_login").innerHTML = "Такого EMAIL не существует!"; .document.getElementById('login-submit') .disabled = true; } } }; } </script> 
  • remove the first point in .document.getElementById('login-submit').disabled in both cases, and do not place the code in one line with the conditions in if and else - Igor
  • Funny, the parser should have broken even at the point before the document . Yes, in chrome. I broke, your error could not be reproduced. - user207618

1 answer 1

Remove the first point in .document.getElementById('login-submit').disabled in both cases, and do not place the code on one line with the conditions in if and else :

 if (xmlhttp.responseText) { document.getElementById('login-submit').disabled = false; } else { document.getElementById("check_login").innerHTML = "Такого EMAIL не существует!"; document.getElementById('login-submit').disabled = true; }