diff options
author | Andinus <andinus@nand.sh> | 2021-09-26 17:21:15 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-09-26 17:21:15 +0530 |
commit | 74eb196a74f410754cd0ac3300ac432463df90b9 (patch) | |
tree | cb707d48b2e64257ca2bdc14abd44b621ae9014d /landing/assets/vendor/php-email-form | |
parent | e62f7f13b2602c4764ff949c6d73d9b7dae456c2 (diff) | |
download | MyMedMemoir-74eb196a74f410754cd0ac3300ac432463df90b9.tar.gz |
Add landing page
Diffstat (limited to 'landing/assets/vendor/php-email-form')
-rw-r--r-- | landing/assets/vendor/php-email-form/validate.js | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/landing/assets/vendor/php-email-form/validate.js b/landing/assets/vendor/php-email-form/validate.js new file mode 100644 index 0000000..64e969c --- /dev/null +++ b/landing/assets/vendor/php-email-form/validate.js @@ -0,0 +1,85 @@ +/** +* PHP Email Form Validation - v3.1 +* 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'); + } + +})(); |