From 2627ee59e3ab0e4a559601cf79ccfdca5f9f6b57 Mon Sep 17 00:00:00 2001 From: Meira Tompkins Date: Fri, 27 Mar 2020 01:06:05 -0400 Subject: [PATCH] done ayyaayayya --- week-9/form.html | 74 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/week-9/form.html b/week-9/form.html index 44978b0..e9c9a04 100644 --- a/week-9/form.html +++ b/week-9/form.html @@ -83,22 +83,72 @@ function actionForm() }; -const myForm = document.getElementById('myForm'); +const formDataToJson = formData => { + const entries = formData.entries(); -myForm.addEventListener('submit', function(e) { - e.preventDefault(); + const dataObj = Array.from(entries).reduce( (data, [key, value]) => { + data[key] = value; + if (key === 'email') { + data._replyTo = value; + } + return data; + }, {}); + + return JSON.stringify(dataObj); + }; + + const toggleForm = bool => { + FORM_ELEMENT.reset(); + if (bool) { + FORM_ELEMENT.style.display = 'block'; + SUCCESS_ELEMENT.style.display = 'none'; + } + else { + FORM_ELEMENT.style.display = 'none'; + SUCCESS_ELEMENT.style.display = 'block'; + } + } +const YOUR_FORMSPREE_EMAIL = 'mtompkins09@yahoo.com'; + +const FORMSPREE_POST_URL = `https://formspree.io/mgezqgzj${ YOUR_FORMSPREE_EMAIL }`; + +const FORM_ELEMENT = document.getElementById('myForm'); - const formData = new FormData(this); +const SUCCESS_ELEMENT = document.getElementById('my-form-status'); - fetch("https://formspree.io/mgezqgzj", { - method: 'POST', - headers: {'Content-Type': 'application/x-www-form-url-encoded', 'Accept': 'application/json'}, - body: formData - }) - .then(response => console.log(response)) - .catch(error => console.log(error)) -}); + FORM_ELEMENT.addEventListener('submit', function (e) { + e.preventDefault(); + + const formData = new FormData(this); + formData.append('_subject', 'Submission In Your Face'); + + + fetch("https://formspree.io/mgezqgzj", { + method: 'POST', + body: formDataToJson(formData), + headers: { + 'Content-Type': 'application/json' + } + }) + .catch(e => { + + }) + .then(r => r.json()) + .then(response => { + + if (response.success === 'email sent') { + console.log(response); + toggleForm(); + } + else { + + } + }); + + }); + +