Skip to content

Commit 7f80bed

Browse files
committed
add HTML form submission to netlify
1 parent 79c8f29 commit 7f80bed

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/js/08-feedback.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,27 @@
3333
feedbackFormSubmitButton.addEventListener('click', submitFeedback)
3434

3535
function submitFeedback (e) {
36+
e.preventDefault()
3637
feedbackFormSubmitButton.classList.add('selected')
3738

38-
// TODO: Submit feedback to backend, alternatively let netlify handle it
39+
var feedbackFormOptionButtonSelected = document.querySelector('.feedback-form-button.selected')
40+
if (!feedbackFormOptionButtonSelected) return
41+
42+
const feedbackForm = document.querySelector('.feedback-section form')
43+
if (!feedbackForm) return
44+
const feedbackFormData = new FormData(feedbackForm)
45+
46+
feedbackFormData.append('feedback-option', feedbackFormOptionButtonSelected.value)
47+
48+
console.log(feedbackFormData)
49+
50+
fetch("/", {
51+
method: "POST",
52+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
53+
body: new URLSearchParams(feedbackFormData).toString()
54+
})
55+
.then(() => console.log("Form successfully submitted"))
56+
.catch(error => alert(error))
3957

4058
feedbackFormSubmitButton.innerText = 'Submitted'
4159
feedbackFormSubmitButton.disabled = true

src/partials/feedback-form.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</label>
88
</div>
99
<div class="feedback-form-options">
10-
<button class="feedback-form-button" type="button" name="feedback-yes" aria-label="The article was helpful">Yes</button>
11-
<button class="feedback-form-button" type="button" name="feedback-no" aria-label="The article was not helpful">No</button>
10+
<button class="feedback-form-button" type="button" value="page-helpful" aria-label="The page was helpful">Yes</button>
11+
<button class="feedback-form-button" type="button" value="page-not-helpful" aria-label="The page was not helpful">No</button>
1212
</div>
1313
<div class="feedback-form-text hide">
1414
<textarea name="feedback-text" placeholder="Tell us what you liked or didn't like (optional)" rows="3"></textarea>

0 commit comments

Comments
 (0)