-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9847a10
commit 8e8e764
Showing
1 changed file
with
54 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,54 @@ | ||
/** | ||
* PHP Email Form Validation - v3.8 | ||
* URL: https://bootstrapmade.com/php-email-form/ | ||
* Author: BootstrapMade.com | ||
*/ | ||
(function () { | ||
"use strict"; | ||
|
||
let forms = document.querySelectorAll('.php-email-form'); | ||
|
||
forms.forEach( function(e) { | ||
e.addEventListener('submit', function(event) { | ||
event.preventDefault(); | ||
|
||
let thisForm = this; | ||
|
||
let action = thisForm.getAttribute('action'); | ||
let recaptcha = thisForm.getAttribute('data-recaptcha-site-key'); | ||
|
||
if( ! action ) { | ||
displayError(thisForm, 'The form action property is not set!'); | ||
return; | ||
} | ||
thisForm.querySelector('.loading').classList.add('d-block'); | ||
thisForm.querySelector('.error-message').classList.remove('d-block'); | ||
thisForm.querySelector('.sent-message').classList.remove('d-block'); | ||
|
||
let formData = new FormData( thisForm ); | ||
|
||
if ( recaptcha ) { | ||
if(typeof grecaptcha !== "undefined" ) { | ||
grecaptcha.ready(function() { | ||
try { | ||
grecaptcha.execute(recaptcha, {action: 'php_email_form_submit'}) | ||
.then(token => { | ||
formData.set('recaptcha-response', token); | ||
php_email_form_submit(thisForm, action, formData); | ||
}) | ||
} catch(error) { | ||
displayError(thisForm, error); | ||
} | ||
}); | ||
} else { | ||
displayError(thisForm, 'The reCaptcha javascript API url is not loaded!') | ||
} | ||
} else { | ||
php_email_form_submit(thisForm, action, formData); | ||
} | ||
}); | ||
}); | ||
|
||
function php_email_form_submit(thisForm, action, formData) { | ||
fetch(action, { | ||
method: 'POST', | ||
body: formData, | ||
headers: {'X-Requested-With': 'XMLHttpRequest'} | ||
}) | ||
.then(response => { | ||
if( response.ok ) { | ||
return response.text(); | ||
} else { | ||
throw new Error(`${response.status} ${response.statusText} ${response.url}`); | ||
} | ||
}) | ||
.then(data => { | ||
thisForm.querySelector('.loading').classList.remove('d-block'); | ||
if (data.trim() == 'OK') { | ||
thisForm.querySelector('.sent-message').classList.add('d-block'); | ||
thisForm.reset(); | ||
} else { | ||
throw new Error(data ? data : 'Form submission failed and no error message returned from: ' + action); | ||
} | ||
}) | ||
.catch((error) => { | ||
displayError(thisForm, error); | ||
}); | ||
} | ||
|
||
function displayError(thisForm, error) { | ||
thisForm.querySelector('.loading').classList.remove('d-block'); | ||
thisForm.querySelector('.error-message').innerHTML = error; | ||
thisForm.querySelector('.error-message').classList.add('d-block'); | ||
} | ||
|
||
})(); | ||
|
||
(function () { | ||
"use strict"; | ||
|
||
// Function to handle form submission | ||
function handleFormSubmit(event) { | ||
event.preventDefault(); // Prevent default form submission | ||
|
||
const form = event.target; | ||
const formData = new FormData(form); | ||
const action = form.getAttribute('action'); | ||
|
||
// Show loading message and hide other messages | ||
form.querySelector('.loading').classList.add('d-block'); | ||
form.querySelector('.error-message').classList.remove('d-block'); | ||
form.querySelector('.sent-message').classList.remove('d-block'); | ||
|
||
// Send form data using fetch | ||
fetch(action, { | ||
method: 'POST', | ||
body: formData, | ||
headers: { | ||
'Accept': 'application/json' | ||
} | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
form.querySelector('.loading').classList.remove('d-block'); | ||
|
||
// Check if the response indicates success | ||
if (data.ok) { | ||
form.querySelector('.sent-message').classList.add('d-block'); | ||
form.reset(); // Reset the form | ||
} else { | ||
// Display error message | ||
form.querySelector('.error-message').innerText = data.message || 'Form submission failed.'; | ||
form.querySelector('.error-message').classList.add('d-block'); | ||
} | ||
}) | ||
.catch(error => { | ||
// Hide loading message and display error | ||
form.querySelector('.loading').classList.remove('d-block'); | ||
form.querySelector('.error-message').innerText = error.message; | ||
form.querySelector('.error-message').classList.add('d-block'); | ||
}); | ||
} | ||
|
||
// Attach event listener to forms with class 'php-email-form' | ||
document.querySelectorAll('.php-email-form').forEach(form => { | ||
form.addEventListener('submit', handleFormSubmit); | ||
}); | ||
|
||
})(); | ||
|