Код: Выделить всё
...
...
...
Request An Appointment!
-- Location --
X_____X
X_____X
X_____X
Submit
[img]images/loader.gif[/img]
SUCCESS: Your Request Has Been Sent!
ERROR: Please [url=tel:+X_________X]Call[/url] Or
[url=mailto:X________X]Email[/url] Me Instead.
< /code>
submit-appt.js
const apptReq = document.getElementById('appointment');
apptReq.addEventListener('submit', (e) => {
e.preventDefault();
let aname = document.getElementById('appt-name').value;
let anameError = document.getElementById('appt-nameError');
let aphone = document.getElementById('appt-phone').value;
let aphoneError = document.getElementById('appt-phoneError');
let adate = document.getElementById('appt-date').value;
let adateError = document.getElementById('appt-dateError');
let alocation = document.getElementById('appt-location').value;
let alocationError = document.getElementById('appt-locationError');
let asuccessMsg = document.getElementById('appt-successMsg');
let afailureMsg = document.getElementById('appt-failureMsg');
let asubButton = document.getElementById('appt-button');
let aloader = document.getElementById('appt-loader');
anameError.textContent = '';
aphoneError.textContent = '';
adateError.textContent = '';
alocationError.textContent = '';
if (!aname) return anameError.textContent = 'Please enter your name';
if (!aphone) return aphoneError.textContent = 'Please enter a phone number';
if (!validatePhoneNumber(aphone)) return aphoneError.textContent = 'Please enter a valid number';
if (!adate) return adateError.textContent = 'Please request an appt date';
if (Date.parse(`${adate}T00:00:00.000`) < Date.now()) return adateError.textContent = 'Please request a future appt date';
if (!alocation) return alocationError.textContent = 'Please select a clinic';
const data = new FormData(apptReq);
asubButton.style.display = 'none';
aloader.style.display = 'block';
postData();
async function postData() {
try {
const response = await fetch('_private/php_scripts/submit-appt.php', {
method: 'POST',
body: data,
})
if (!response.ok) {
aloader.style.display = 'none';
asubButton.style.display = 'block';
afailureMsg.style.display = 'block';
} else {
aloader.style.display = 'none';
asubButton.style.display = 'block';
asuccessMsg.style.display = 'block';
}
} catch (error) {
aloader.style.display = 'none';
asubButton.style.display = 'block';
afailureMsg.style.display = 'block';
}
}
});
function validatePhoneNumber() {
let regEx = /^\+?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if (aphone.match(regEx)) {
return true;
} else {
return false;
}
};
< /code>
submit-appt.php
Подробнее здесь: [url]https://stackoverflow.com/questions/79422125/why-does-my-async-form-fail-to-post-inputs-to-php-file-from-safari-but-not-chrom[/url]