Skip to content

Commit

Permalink
added backend for feedback form
Browse files Browse the repository at this point in the history
  • Loading branch information
Sawan-Kushwah committed Nov 6, 2024
1 parent 42cf4b5 commit 49be790
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
42 changes: 31 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2507,7 +2507,7 @@ <h2 class="modal-header">Success!</h2>
});

// Handle form submission and show modal
document.getElementById('feedback-form').addEventListener('submit', function (e) {
document.getElementById('feedback-form').addEventListener('submit', async function (e) {
e.preventDefault();

// Get form values
Expand Down Expand Up @@ -2540,21 +2540,41 @@ <h2 class="modal-header">Success!</h2>
// Store updated feedback array back in localStorage
localStorage.setItem('feedbacks', JSON.stringify(feedbackData));

// Clear form inputs
document.getElementById('feedback-form').reset();
// Send data to the server using Fetch API
try {
const response = await fetch('http://localhost:5000/api/feedback/saveFeedback', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(feedback)
});

if (!response.ok) {
throw new Error('Failed to submit feedback');
}

// Clear form inputs
document.getElementById('feedback-form').reset();

// Reset emoji description
emojiDescription.textContent = 'Select an option';
// Reset emoji description
emojiDescription.textContent = 'Select an option';

// Show success modal
document.getElementById('successModal').style.display = 'flex';
// Show success modal
document.getElementById('successModal').style.display = 'flex';

// Close modal after 3 seconds
setTimeout(function () {
document.getElementById('successModal').style.display = 'none';
}, 3000);
// Close modal after 3 seconds
setTimeout(function () {
document.getElementById('successModal').style.display = 'none';
}, 3000);

} catch (error) {
// Handle errors, for example if fetch fails
alert('There was an error submitting your feedback. Please try again later.');
}
});


// Close the modal when the 'Dismiss' button is clicked or 'X' is pressed
document.getElementById('dismissModal').addEventListener('click', function () {
document.getElementById('successModal').style.display = 'none';
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/feedbackController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function saveFeedback(req, res) {

// Create new contact document
const newFeedback = new Feedback({ name, email, rating, comments });

console.log(newFeedback)
// Save contact form data to the database
await newFeedback.save();

Expand Down

0 comments on commit 49be790

Please sign in to comment.