Skip to content

Instantly share code, notes, and snippets.

@porfidev
Created June 3, 2019 21:34
Show Gist options
  • Save porfidev/9fae18b523a78df763486c2d416958cd to your computer and use it in GitHub Desktop.
Save porfidev/9fae18b523a78df763486c2d416958cd to your computer and use it in GitHub Desktop.
function showMessage(message){
const processDiv = document.getElementById('process');
processDiv.innerHTML = message;
processDiv.style.display = 'block';
}
function hideMessage() {
const processDiv = document.getElementById('process');
processDiv.style.display = 'none';
}
function showForm() {
const contactForm = document.getElementById('contactForm');
contactForm.style.display = 'block';
}
function hideForm() {
const contactForm = document.getElementById('contactForm');
contactForm.style.display = 'none';
}
function sendAjaxForm(form){
const formData = new FormData(form);
const request = new XMLHttpRequest();
request.addEventListener('load', function(){
showMessage('Mensaje enviado, gracias!');
setTimeout(function(){
showForm();
hideMessage();
}, 3000);
});
request.addEventListener('error', function(){
showMessage('Ha ocurrido un error, intenta más tarde');
});
request.open('post', 'send-email.php');
request.send(formData);
}
function sendEmail(event) {
event.preventDefault();
showMessage('procesando solicitud');
hideForm();
sendAjaxForm(contactForm);
}
const contactForm = document.getElementById('contactForm');
contactForm.addEventListener('submit', sendEmail);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment