From d65b7a0d3590bb26461afc489084ba48caa826a1 Mon Sep 17 00:00:00 2001 From: Ruslan Date: Thu, 14 May 2026 06:52:20 +0000 Subject: [PATCH] Fix submit forms: use getElementById instead of stale closures, fix texts --- app/templates/login.html | 104 +++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 49 deletions(-) diff --git a/app/templates/login.html b/app/templates/login.html index f386925..41b8648 100644 --- a/app/templates/login.html +++ b/app/templates/login.html @@ -193,11 +193,19 @@ } async function submitForm() { - const name = document.getElementById('am-name').value.trim(); - const company = document.getElementById('am-company').value.trim(); - const email = document.getElementById('am-email').value.trim(); - const phone = document.getElementById('am-phone').value.trim(); - const manager = document.getElementById('am-manager').value.trim(); + const nameEl = document.getElementById('am-name'); + const companyEl = document.getElementById('am-company'); + const emailEl = document.getElementById('am-email'); + const phoneEl = document.getElementById('am-phone'); + const managerEl = document.getElementById('am-manager'); + const submitBtn = document.getElementById('am-submit'); + const errorEl = document.getElementById('am-error'); + + const name = nameEl ? nameEl.value.trim() : ''; + const company = companyEl ? companyEl.value.trim() : ''; + const email = emailEl ? emailEl.value.trim() : ''; + const phone = phoneEl ? phoneEl.value.trim() : ''; + const manager = managerEl ? managerEl.value.trim() : ''; const checked = [...document.querySelectorAll('#am-products input[type=checkbox]:checked')]; const products = checked.map(c => c.value); @@ -205,31 +213,25 @@ const phoneRe = /^[\+\d][\d\s\-\(\)]{6,18}$/; const fields = [ - { el: document.getElementById('am-name'), val: name, check: () => !!name, msg: 'Введите имя и фамилию' }, - { el: document.getElementById('am-company'), val: company, check: () => !!company, msg: 'Введите название компании' }, - { el: document.getElementById('am-email'), val: email, check: () => emailRe.test(email), msg: 'Введите корректный email' }, - { el: document.getElementById('am-phone'), val: phone, check: () => phoneRe.test(phone), msg: 'Введите корректный номер телефона' }, + { el: nameEl, check: () => !!name, msg: 'Введите имя и фамилию' }, + { el: companyEl, check: () => !!company, msg: 'Введите название компании' }, + { el: emailEl, check: () => emailRe.test(email), msg: 'Введите корректный email' }, + { el: phoneEl, check: () => phoneRe.test(phone), msg: 'Введите корректный номер телефона' }, ]; const errors = []; fields.forEach(f => { - if (!f.check()) { - f.el.classList.add('am-invalid'); - errors.push(f.msg); - } else { - f.el.classList.remove('am-invalid'); - } + if (f.el && !f.check()) { f.el.classList.add('am-invalid'); errors.push(f.msg); } + else if (f.el) f.el.classList.remove('am-invalid'); }); if (errors.length) { - errEl.textContent = errors.join(' • '); - errEl.style.display = 'block'; + if (errorEl) { errorEl.textContent = errors.join(' • '); errorEl.style.display = 'block'; } return; } - btnSubmit.disabled = true; - btnSubmit.textContent = 'Отправка...'; - errEl.style.display = 'none'; + if (submitBtn) { submitBtn.disabled = true; submitBtn.textContent = 'Отправка...'; } + if (errorEl) errorEl.style.display = 'none'; try { const res = await fetch('/api/request-access', { @@ -241,10 +243,8 @@ const d = await res.json().catch(() => ({})); throw new Error(d.detail || 'Ошибка отправки'); } - btnSubmit.textContent = 'Отправлено!'; - // Show success message - const body = document.querySelector('.access-modal-body'); - const footer = document.querySelector('.access-modal-footer'); + const body = document.querySelector('#access-modal .access-modal-body'); + const footer = document.querySelector('#access-modal .access-modal-footer'); body.innerHTML = '
' + '
' + '
Запрос отправлен
' + @@ -252,10 +252,10 @@ '
'; footer.innerHTML = ''; } catch(e) { - errEl.textContent = e.message || 'Ошибка отправки, попробуйте позже'; - errEl.style.display = 'block'; - btnSubmit.disabled = false; - btnSubmit.textContent = 'Запросить доступ'; + const eb = document.getElementById('am-error'); + if (eb) { eb.textContent = e.message || 'Ошибка отправки, попробуйте позже'; eb.style.display = 'block'; } + const sb = document.getElementById('am-submit'); + if (sb) { sb.disabled = false; sb.textContent = 'Запросить доступ'; } } } @@ -288,7 +288,7 @@
Связаться с Русланом
-
Напишите — отвечу в ближайшее время
+
@@ -357,35 +357,41 @@ window._closeContactModal = closeContact; async function submitContact() { - const name = document.getElementById('cm-name').value.trim(); - const email = document.getElementById('cm-email').value.trim(); - const phone = document.getElementById('cm-phone').value.trim(); - const text = document.getElementById('cm-text').value.trim(); + const nameEl = document.getElementById('cm-name'); + const emailEl = document.getElementById('cm-email'); + const phoneEl = document.getElementById('cm-phone'); + const textEl = document.getElementById('cm-text'); + const submitBtn = document.getElementById('cm-submit'); + const errorEl = document.getElementById('cm-error'); + + const name = nameEl ? nameEl.value.trim() : ''; + const email = emailEl ? emailEl.value.trim() : ''; + const phone = phoneEl ? phoneEl.value.trim() : ''; + const text = textEl ? textEl.value.trim() : ''; const emailRe = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const phoneRe = /^[\+\d][\d\s\-\(\)]{6,18}$/; const fields = [ - { el: document.getElementById('cm-name'), check: () => !!name, msg: 'Введите имя' }, - { el: document.getElementById('cm-email'), check: () => emailRe.test(email), msg: 'Введите корректный email' }, - { el: document.getElementById('cm-phone'), check: () => phoneRe.test(phone), msg: 'Введите корректный номер телефона' }, - { el: document.getElementById('cm-text'), check: () => !!text, msg: 'Введите сообщение' }, + { el: nameEl, check: () => !!name, msg: 'Введите имя' }, + { el: emailEl, check: () => emailRe.test(email), msg: 'Введите корректный email' }, + { el: phoneEl, check: () => phoneRe.test(phone), msg: 'Введите корректный номер телефона' }, + { el: textEl, check: () => !!text, msg: 'Введите сообщение' }, ]; const errors = []; fields.forEach(f => { - if (!f.check()) { f.el.classList.add('am-invalid'); errors.push(f.msg); } - else f.el.classList.remove('am-invalid'); + if (f.el && !f.check()) { f.el.classList.add('am-invalid'); errors.push(f.msg); } + else if (f.el) f.el.classList.remove('am-invalid'); }); + if (errors.length) { - errEl.textContent = errors.join(' • '); - errEl.style.display = 'block'; + if (errorEl) { errorEl.textContent = errors.join(' • '); errorEl.style.display = 'block'; } return; } - btnSubmit.disabled = true; - btnSubmit.textContent = 'Отправка...'; - errEl.style.display = 'none'; + if (submitBtn) { submitBtn.disabled = true; submitBtn.textContent = 'Отправка...'; } + if (errorEl) errorEl.style.display = 'none'; try { const res = await fetch('/api/contact', { @@ -401,15 +407,15 @@ '
' + '
' + '
Отправлено
' + - '
Сообщение получено — свяжемся с вами в ближайшее время
' + + '
Постараюсь ответить в ближайшее время
' + '
'; document.getElementById('cm-footer').innerHTML = ''; } catch(e) { - errEl.textContent = e.message || 'Ошибка отправки, попробуйте позже'; - errEl.style.display = 'block'; - btnSubmit.disabled = false; - btnSubmit.textContent = 'Отправить'; + const eb = document.getElementById('cm-error'); + if (eb) { eb.textContent = e.message || 'Ошибка отправки, попробуйте позже'; eb.style.display = 'block'; } + const sb = document.getElementById('cm-submit'); + if (sb) { sb.disabled = false; sb.textContent = 'Отправить'; } } }