Quiz calculation not working
-
I have a quiz form created with Forminator. It has 15 questions with 3 options with radio field. Radio option has values 1, 2, and 3 for all questions. At the end I need to calculate which options the user selected most and based on that show the answers.
For example: if the user selected most 1s then answer will be "Kapha", for 2 most selected it will be "Pitta" and for 3s it will be "Vatta"
I have created three hidden fields to store 1s, 2s, and 3s answers count. And hidden-4 will store the result. And this hidden-4 will be shown as a result to the user and also need to this in email.
This is the JS I am using in my footer.php before </body> tag. This JS working fine in console.log displaying the results. but no answer is storing and sending to emails.
<script>
function initializeDoshaScript() {
const form = document.querySelector('form.forminator-custom-form');
if (!form) {
console.log("❌ Form not found yet. Retrying...");
setTimeout(initializeDoshaScript, 300);
return;
}
console.log("✅ Form found. Attaching submit handler.");
form.onsubmit = function () {
console.log("🟢 Form is about to submit...");
let countOne = 0;
let countTwo = 0;
let countThree = 0;
const selectedRadios = form.querySelectorAll('input[type="radio"]:checked');
console.log("📌 Selected radios:", selectedRadios);
selectedRadios.forEach(function (radio) {
const val = radio.value.trim().toLowerCase();
console.log('Value in each: ',val);
if (val == 1) countOne++;
else if (val == 2) countTwo++;
else if (val == 3) countThree++;
});
const oneField = form.querySelector('input[name="hidden-1"]');
const twoField = form.querySelector('input[name="hidden-2"]');
const threeField = form.querySelector('input[name="hidden-3"]');
const resultField = form.querySelector('input[name="hidden-4"]');
console.log('resultField: ', resultField);
if (oneField && twoField && threeField ) {
oneField.value = countOne;
twoField.value = countTwo;
threeField.value = countThree;
let result = '';
if (countOne > countTwo && countOne > countThree && resultField) {
result = 'Vata';
} else if (countTwo > countOne && countTwo > countThree) {
result = 'Pitta';
} else if (countThree > countOne && countThree > countTwo) {
result = 'Kapha';
} else {
result = 'Mixed Body Type';
}
resultField.value = result;
console.log('resultField: ', resultField.value);
console.log("🔢 one:", countOne, "two:", countTwo, "three:", countThree);
console.log("🧘 Dosha result:", result);
} else {
console.log("⚠️ Hidden fields not found!");
}
// Let the form submit as usual
return true;
};
}
document.addEventListener('DOMContentLoaded', initializeDoshaScript);
</script>The page I need help with: [log in to see the link]
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘Quiz calculation not working’ is closed to new replies.