Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Put all the element of a mysql table into javascript array

I would like to store my mysql table into a javascript array. I wish to push into my array every element of the table so I would be left with an array like this :

array[0] = [question: information01, answer: information02...]
array[1] = [question: information11, answer: information12...]

With my following code, all the elements of my table are stocked into the first element of my array, which gives me this :

array[0] = [[question: information01, answer: information02...], [question: information11, answer: information12...]] 

and so the array has a size of 0 instead of size of the mysql table (I hope it makes sense)

var availableQuestions = [];
ajaxRequest("GET","../controller.php?func=get_enigme", (enigme) => {
    var enigmeAll = JSON.parse(enigme);
    for (var i=0; i < enigmeAll.length; i++){
        availableQuestions.push(enigmeAll[i]);
    }
});
function ajaxRequest(type,url,callback,data=""){
let request = new XMLHttpRequest();
request.open(type, url);

request.onload = function () {
    switch (request.status){
        case 200:
        case 201: //console.log(request.responseText);
                    callback(request.responseText);
            break;
        default: console.log(request.status);
    }
};

request.send(data);
}

When I do a console.log(availableQuestions.length) in my ajaxRequest it is saying 2, but when I do it outside of my function it is saying length = 0. Which I don't understand. Does anyone have an answer to my problem ? Thank you very much in advance for your answers,

EDIT : I just put my parse into another array with my for loop which of course didn't help..

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

var availableQuestions = [];
ajaxRequest("GET","../controller.php?func=get_enigme", (enigme) => {
    var enigmeAll = JSON.parse(enigme);
    for (var i=0; i < enigmeAll.length; i++){
        availableQuestions.push([enigmeAll[i]]);
    }
});

try this

about 4 years ago · Juan Pablo Isaza Denunciar

0

The response you are getting is not in the right format for your parsing to work. You would probably want to end up with something like:

[
    {
        id: 1,
        question: "Question1"
        answer: "Answer1"
    },
    {
        id: 2,
        question: "Question2"
        answer: "Answer2"
    }
]

For that to happen, the response you get from the ajax call should look similar. If you have control over how the response is built, it would be better to go ahead and modify it. If not, you're gonna need to extend the logic of parsing that response a bit, since it contains a lot of tokens that aren't really necessary (I'm seeing a lot of indexes as keys, probably as a result of stringifying some raw data that comes from the table).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda