@PlasterTom

Почему скрипт отправки формы рушит другие скрипты?

Без него другой код отлично работает.
(function () {
        document.addEventListener("DOMContentLoaded", function(){
        	document.forms["contact-form"].addEventListener("submit", postData);
        });
        
        function postData(formsubmission){
        
        	var userName = encodeURIComponent(document.getElementById("username").value);
        	var email = encodeURIComponent(document.getElementById("email-address").value);
        	var phone = encodeURIComponent(document.getElementById("phone-number").value);
            var day = encodeURIComponent(document.getElementById("datepicker").value);
        	var message = encodeURIComponent(document.getElementById("message").value);
        
        	// Checks if fields are filled-in or not, returns response "<p>Please enter your details.</p>" if not.
        	if(userName == "" || phone == ""){
        		document.getElementById("response").innerHTML = "<p>Пожалуйста, введите ваши данные</p>";
        		return;
        	}
        
        	// Parameters to send to PHP script. The bits in the "quotes" are the POST indexes to be sent to the PHP script.
        	var params = "username=" + userName + "&email=" + email + "&phone=" + phone + "&day=" + day  + "&message=" + message;
        
        	var http = new XMLHttpRequest();
        	http.open("POST","send.php",true);
        
        	// Set headers
        	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        
        	http.onreadystatechange = function(){
        		if(http.readyState == 4 && http.status == 200){
                    if (http.responseText ==='Error') {
                    document.getElementById("response").className('error');
                } else if (http.responseText ==='Success'){
                    document.getElementById("response").className('sucsses');
        		}
        	}
        	http.send(params);
        	formsubmission.preventDefault();
        }
})();
  • Вопрос задан
  • 124 просмотра
Решения вопроса 1
profesor08
@profesor08 Куратор тега JavaScript
А ты открой консоль и посмотри на ошибки.
Тебе надо проверить эти потенциальные места, где может возникнуть ошибка.
EQpgHHbMS76nelo-K53KjQ.png
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы