EoQ_Supporting_Files/insertQualifiers.js

95 lines
2.6 KiB
JavaScript

function insertQualifiers(){
//alert("qualifying....");
surveyId = document.getElementById("surveyId").value;
if (/^SV_[a-zA-Z0-9]+$/.test(surveyId)) {
console.log("Valid: starts with SV_ and followed by letters/numbers only");
console.log(surveyId);
} else {
console.log("Invalid: must start with SV_ and be followed by only letters or numbers");
alert("Invalid survey ID");
return;
}
// Create the form data object
const formData = new FormData();
formData.append('surveyId', surveyId);
// Send the form data using fetch
fetch('get_qualifiers.php', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(data => {
// Output result
console.log(data);
const parsed = JSON.parse(data);
console.log (parsed["1"]);
document.querySelector('label[for="location"]').textContent = parsed["1"][0];
// .shift(); //
parsed["1"][0] = "Any";
select = document.getElementById('location');
optionCount = select.options.length;
if (optionCount > 1) {return;}
select.innerHTML = ''; // removes all option elements inside
parsed["1"].forEach((loc, index) => {
const option = document.createElement("option");
option.value = index;
option.textContent = loc;
select.appendChild(option);
});
document.querySelector('label[for="level"]').textContent = "New question 2";
// .shift(); //
parsed["2"][0] = "Any";
select = document.getElementById('level');
optionCount = select.options.length;
if (optionCount > 1) {return;}
select.innerHTML = ''; // removes all option elements inside
parsed["2"].forEach((loc, index) => {
const option = document.createElement("option");
option.value = index;
option.textContent = loc;
select.appendChild(option);
});
document.querySelector('label[for="gov"]').textContent = "New question 3";
parsed["3"][0] = "Any";
select = document.getElementById('gov');
optionCount = select.options.length;
if (optionCount > 1) {return;}
select.innerHTML = ''; // removes all option elements inside
parsed["3"].forEach((loc, index) => {
const option = document.createElement("option");
option.value = index;
option.textContent = loc;
select.appendChild(option);
});
/*document.getElementById("record_no").textContent = total;
if (total < 9){
return;
}*/
// return data; // Should be an array like [{ value: -3, count: 2 }, ..., { value: 3, count: 5 }]
})
.catch(error => {
console.error('Error:', error);
});
}