Javascript 如何将数据库生成的php数组转换成ajax变量
我有五个来自数据库的问题。我需要存储所有的问题;每个选项和正确答案都作为一个数组。但是下面的脚本将每个问题及其选项存储为单个数组-我有5个数组而不是1个。 实现这一目标的最佳方式是什么;我可以对脚本做什么修改 问题.jsJavascript 如何将数据库生成的php数组转换成ajax变量,javascript,php,ajax,Javascript,Php,Ajax,我有五个来自数据库的问题。我需要存储所有的问题;每个选项和正确答案都作为一个数组。但是下面的脚本将每个问题及其选项存储为单个数组-我有5个数组而不是1个。 实现这一目标的最佳方式是什么;我可以对脚本做什么修改 问题.js var ajax = new XMLHttpRequest(); ajax.open("GET", "data.php", true); ajax.send(); ajax.onreadystatechange = function () { if (this.read
var ajax = new XMLHttpRequest();
ajax.open("GET", "data.php", true);
ajax.send();
ajax.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
var data = JSON.parse(this.responseText);
console.log(data);
for (var a = 0; a < data.length; a++) {
var question_text = data[a].question_text;
var choice1 = data[a].choice1;
var choice2 = data[a].choice2;
var choice3 = data[a].choice3;
var choice4 = data[a].choice4;
var answer = data[a].correctchoice;
(function () {
var questions = [{
question: question_text,
choices: [choice1, choice2, choice3, choice4]
}];
var correctAnswer = answer;
console.log('question: ' + question_text, 'a: ' + choice1, 'b: ' + choice2, 'c: ' + choice3, 'd: ' + choice4, 'answer: ' + correctAnswer);
var ajax=new-XMLHttpRequest();
open(“GET”,“data.php”,true);
ajax.send();
ajax.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
var data=JSON.parse(this.responseText);
控制台日志(数据);
对于(var a=0;a
您必须在外部范围中定义问题
数组,然后将每个问题推送到for
循环中:
// Define an outer scope array here so you can use it later
var questions = [];
var ajax = new XMLHttpRequest();
ajax.open("GET", "data.php", true);
ajax.send();
ajax.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
var data = JSON.parse(this.responseText);
for (var a = 0; a < data.length; a++) {
var question_text = data[a].question_text;
var choice1 = data[a].choice1;
var choice2 = data[a].choice2;
var choice3 = data[a].choice3;
var choice4 = data[a].choice4;
var answer = data[a].correctchoice;
// Push a new question to the questions array
questions.push({
question: question_text,
choices: [choice1, choice2, choice3, choice4],
correctAnswer: answer
});
}
// You'll have the array filled with each item to one question with all the options and the correctAnswer
console.log('questions', questions);
}
}
//在此处定义外部作用域数组,以便以后使用
var问题=[];
var ajax=new-XMLHttpRequest();
open(“GET”,“data.php”,true);
ajax.send();
ajax.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
var data=JSON.parse(this.responseText);
对于(var a=0;a
您必须在外部范围中定义问题
数组,然后将每个问题推送到for
循环中:
// Define an outer scope array here so you can use it later
var questions = [];
var ajax = new XMLHttpRequest();
ajax.open("GET", "data.php", true);
ajax.send();
ajax.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
var data = JSON.parse(this.responseText);
for (var a = 0; a < data.length; a++) {
var question_text = data[a].question_text;
var choice1 = data[a].choice1;
var choice2 = data[a].choice2;
var choice3 = data[a].choice3;
var choice4 = data[a].choice4;
var answer = data[a].correctchoice;
// Push a new question to the questions array
questions.push({
question: question_text,
choices: [choice1, choice2, choice3, choice4],
correctAnswer: answer
});
}
// You'll have the array filled with each item to one question with all the options and the correctAnswer
console.log('questions', questions);
}
}
//在此处定义外部作用域数组,以便以后使用
var问题=[];
var ajax=new-XMLHttpRequest();
open(“GET”,“data.php”,true);
ajax.send();
ajax.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
var data=JSON.parse(this.responseText);
对于(var a=0;a
Welcome,新撰稿人John Olayiwola!你的问题很详细。你是在找人写ajax调用吗(我在这里没有看到)?对于您的问题,一般的答案是,如果您在php的结尾使用了一个对$row进行json_编码的回音,那么在接收端,您可以使用JavaScript解析整个json字符串,并得到您想要的内容。这将与const jsrows=json.parse(returnvalfromajax);(然后像任何JS var一样使用它)哦……谢谢@Yishmeray。我真的很感激@Yishmeray。我已经根据您的建议编辑了代码,现在可以从数据库中获取问题。但是出现了一些新的挑战,我在编辑的代码中强调了这些挑战。请帮帮忙。谢谢您将单击处理程序附加到
,而不是
。我建议在
元素上放置您的ID,如next
、prev
和start
。或者,使用
元素,而不是Text
感谢您的回复@Phil。但我认为问题是,我的每个问题和选项都存储为单个数组,因此我有5个不同的数组5个问题,而不是一个数组中的5个问题。我需要帮助将我所有的问题和选项存储为一个数组。欢迎,新撰稿人John Olayiwola!你的问题的细节很好。你是在找人编写ajax调用吗(我在这里没有看到)?对于您的问题,一般的答案是,如果您在php的结尾使用了一个对$row进行json_编码的回音,那么在接收端,您可以使用JavaScript解析整个json字符串,并得到您想要的内容。这将与const jsrows=json.parse(returnvalfromajax);(然后像任何JS var一样使用它)哦……谢谢@Yishmeray。我真的很感激@Yishmeray。我已经根据您的建议编辑了代码,现在可以从数据库中获取问题。但是出现了一些新的挑战,我在编辑的代码中强调了这些挑战。请帮帮忙。谢谢您将单击处理程序附加到
,而不是
。我建议在
元素上放置您的ID,如next
、prev
和start
。或者,使用
元素,而不是Text
感谢您的回复@Phil。但我认为问题在于