Can't wrap my head around the issue im having. It's within a group i am currently working with.
Basically, I am being sent a large array. Elements needed to be worried about are the ID number of each question. As well as the question.
I am putting each value of the question in an array, and the ID in an array in order.
When I add the "question" value into the 'options' tag for HTML, I could only add the text (value) of the option selected. Therefore, if I add random questions.. I can't figure out how to attach an ID. The ID is needed to send to my backend so they can find each question much easier.
Goal: To attach a question and an ID array, when I 'select' the question from a drop down and add the value over to a new array to THEN be sent over to backend.. It is a confusing task and I can't seem to figure out a proper way for it to be done.
The best I have done, was to add the ID onto the end of the 'option' as text. But it certainly isnt nice looking, and would have to somehow make a function to take only the number, of the entire string at the end. When i am trying to just send over an ID with the Question String, making it so they can access the question using the unique ID which each one.
<script>
var testArray=[];
var scoreArray=[];
var questionArray=[];
var idArray=[];
var countBox=1;
var boxName=1;
function filterq(){
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function() {
if (this.readyState == 4) {
document.getElementById('questionSelect').innerText = null
alert(this.responseText);
for(i=0;i<21;i++){
var questionDisplay = document.getElementById("questionSelect");
var options = document.createElement("option");
var data=JSON.parse(this.responseText);
questionArray.push(data['list'][i]['question']);
idArray.push(data['list'][i]['ID']);
options.text=questionArray[i]+" (ID:"+idArray[i]+")";
questionDisplay.add(options);
}
}
}
var qdiff = document.getElementById("qLevel").value;
var qtopic = document.getElementById("qTopic").value;
var qkeyword = document.getElementById("qKeyword").value;
var myObj = {id: "qfilter", topic: qtopic, difficulty: qdiff, keyword: qkeyword};
var myJSON = JSON.stringify(myObj);
ajaxRequest.open("POST","https://web.njit.edu/~rtw3/CS490/betamiddle.php", true);
//ajaxRequest.open("POST", "fronttest.php", true);
ajaxRequest.send(myJSON);
}
function addq(){
//pushing value onto test array
var addingquestion = document.getElementById('questionSelect').value;
testArray.push(addingquestion);
//creating textoutput for each question
var node = document.createElement("LI");
var textnode = document.createTextNode(addingquestion);
node.appendChild(textnode);
document.getElementById("test").appendChild(node);
//adding textboxes each click
var boxName = "q" + countBox;
document.getElementById('test').innerHTML+='<input type="text" id="'+boxName+'" size="1" placeholder="pts" maxlength="2"" /><br/>';
countBox+=1;
}
function examAdd(){
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.getElementById("testLayout").innerHTML = "Exam has been Successfully Created";
}
}
var topics = document.getElementById("qTopic").value;
var keywords = document.getElementById("qKeyword").value;
var testname = document.getElementById("eName").value;
for(i=1; i<countBox; i++){
var theID = document.getElementById("q"+i).value;
scoreArray.push(theID);
}
var myObj = {id:"addt", test: testArray, scores: scoreArray, topic: topics, keyword: keywords, tname: testname};
var myJSON = JSON.stringify(myObj);
var myJSON2 = JSON.stringify(scoreArray);
//ajaxRequest.open("POST","examtest.php", true);
ajaxRequest.open("POST","https://web.njit.edu/~rtw3/CS490/betamiddle.php", true);
ajaxRequest.send(myJSON);
}
function cancel(){
document.getElementById('test').innerHTML=null;
}
</script>
</body>
</html>
Attaching a unique ID to Every question AFTER it has been added to a new array, to then be sent back with each unique ID and Question in order.
note: the id is already with the question when its sent. I just need to save it somehow when i send it back but im unsure how since i send back the .value of an option selected, rather than an array with questions and IDs.