Question: How to save a JavaScript variable value into a database?
Problem: I made a mini JavaScript game where the clicks buttons and the game records the user’s responses in a JavaScript list of string-type responses. This works fine. My issue is now being able to store that list as a response into a database that can be used later to analyze.
I am going to provide the entire code below. But imo, the important part is somehow storing the var viewList into a dataset? I would assume, there is a POST type of method I can implement in my custom function endGame(), which will store it in a dataset.
Code:
Qualtrics.SurveyEngine.addOnload(function()
{
let randomList = d"jelly", "mints", "starburts"];
let orderedList = o"Apple", "Banana", "Coconut"];
let randomListCopy = ...randomList];
let orderedListCopy = l...orderedList];
let viewedList = ];
let value = "";
let gameOn = true;
let round = 1;
let p = .5 //Math.random();
let q = 1 - p;
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function renderPreGame() {
document.getElementById('qChoiceClass').style.visibility = 'hidden';
document.getElementById('pChoiceClass').style.visibility = 'hidden';
document.getElementById('roundID').style.visibility = 'hidden';
document.getElementById('pqResult').style.visibility = 'hidden';
document.getElementById('endGame').style.visibility = 'hidden';
}
function startGame() {
document.getElementById('startGame').style.visibility = 'hidden';
document.getElementById('qChoiceClass').style.visibility = 'visible';
document.getElementById('pChoiceClass').style.visibility = 'visible';
document.getElementById('roundID').style.visibility = 'visible';
document.getElementById('pqResult').style.visibility = 'visible';
document.getElementById('roundID').innerHTML = "Round: " + round;
document.getElementById('pqResult').innerHTML = "Choice: ";
choosingPQ();
}
function endGame() {
document.getElementById('qChoiceClass').style.visibility = 'hidden';
document.getElementById('pChoiceClass').style.visibility = 'hidden';
document.getElementById('roundID').style.visibility = 'visible';
document.getElementById('pqResult').style.visibility = 'visible';
document.getElementById('endGame').style.visibility = 'visible';
document.getElementById('pqResult').innterHTML = "Choice: " + value;
console.log(viewedList);
}
function choosingPQ () {
if (Math.random() < p) {
if (randomList.length === 0) {
randomList = n...randomListCopy];
}
index = getRandomInt(randomList.length);
value = randomListnindex];
console.log(value);
randomList.splice(index, 1);
document.getElementById("pBtnSkip").disabled = false;
document.getElementById("qBtnChoose").disabled = true;
document.getElementById("qBtnLook").disabled = true;
document.getElementById("pqResult").style.background = "rgb(255, 55, 55)";
document.getElementById("pqResult").innerHTML = "Choice: " + value;
viewedList.push(value);
} else {
if (orderedList.length === 0) {
orderedList = o...orderedListCopy];
}
value = orderedList.shift();
console.log(value);
console.log(orderedList.length);
document.getElementById("pBtnSkip").disabled = true;
document.getElementById("qBtnChoose").disabled = false;
document.getElementById("qBtnLook").disabled = false;
document.getElementById("pqResult").style.background = "rgb(86, 193, 86)";
document.getElementById("pqResult").innerHTML = "Choice: " + value;
viewedList.push(value);
}
}
document.getElementById("startGame").addEventListener("click", function() {
startGame();
});
document.getElementById("pBtnSkip").addEventListener("click", function() {
round++;
document.getElementById("roundID").innerHTML = "Round: " + round;
choosingPQ();
});
document.getElementById("qBtnChoose").addEventListener("click", function() {
document.getElementById("roundID").innerHTML = "Round: " + round;
console.log("game has ended");
gameOn = false;
endGame();
});
document.getElementById("qBtnLook").addEventListener("click", function() {
round++;
document.getElementById("roundID").innerHTML = "Round: " + round;
choosingPQ();
});
renderPreGame();
});
Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery("#"+this.questionId+" .InputText").on('keyup',function(){
Qualtrics.SurveyEngine.setEmbeddedData( 'viewList', viewList);
});
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
If you need any extra info, please let me know and any help would greatly be appreciated, thank you!