const surveyForm = document.getElementById('surveyForm');
const votingSection = document.getElementById('votingSection');
const votingForm = document.getElementById('votingForm');
const votingOptions = document.getElementById('votingOptions');
// Handle submission of survey answers
surveyForm.addEventListener('submit', function(event) {
event.preventDefault();
const answers = {
q1: document.getElementById('q1').value,
q2: document.getElementById('q2').value,
q3: document.getElementById('q3').value,
q4: document.getElementById('q4').value,
q5: document.getElementById('q5').value
};
fetch('/submit-answers', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(answers)
})
.then(response => response.json())
.then(data => {
if (data.options) {
// Show voting section with voting options
votingSection.style.display = 'block';
votingOptions.innerHTML = data.options.map((option, index) => `
<div>
<label>
<input type="radio" name="vote" value="${index}" required>
${option}
</label>
</div>
`).join('');
} else {
alert(data.message); // Display waiting message if not enough submissions
}
});
});
// Handle submission of votes
votingForm.addEventListener('submit', function(event) {
event.preventDefault();
const selectedVote = document.querySelector('input[name="vote"]:checked').value;
fetch('/submit-vote', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ vote: selectedVote })
})
.then(response => response.json())
.then(data => {
alert('Thank you for voting!');
});
});