Question

when executing the following code firebug tells me: msj[0] is undefined what is the problem?

What I want to do is that every time it receives a response after every run, catch it in the array after calling msg to generate a single alert to show me all the answers together...

I don't understand why me show undefined if the array has data, in this case has three data..

var msj = [];

for (var a = document.querySelectorAll('table.inventory tbody tr'), i = 0; a[i]; ++i) {
    // get inventory row cells
    cells = a[i].querySelectorAll('span:last-child');
    // buscar la selecion del concepto
    var opciones = cells[0].querySelectorAll('option:checked');
    var value_concept = opciones[0].value;
    // set price as cell[2] * cell[3]
    var Uno = value_concept;
    var Dos = cells[1].innerHTML ;
    var Tres = parseFloatHTML(cells[2]);
    var Cuatro = parseFloatHTML(cells[3]);
    var Cinco = parseFloatHTML(cells[4]);

    var id_fac = id_fac_select;
    var valor_fac = costo_fac_select.split(".");
    var valor_fact = valor_fac[0];
    var valor_recibo = Cinco;

    $.ajax({
        url:"js.php",
        cache:false,
        type:"POST",
        data:{concepto:Uno,descripcion:Dos,valor_total:Tres,valor_pagado:Cuatro,valor_f:valor_fact,saldo_pendiente:valor_recibo,numero_factura:id_fac,id_estudiante:id_student},
        success:function(result)
        {   
            msj.push(result);               
        }
        });

}
console.log(msj);
alert(msj[0]);
Was it helpful?

Solution

You are seeing undefined in the alert() because msj doesn't have data yet. Your code that populates msj is not called until the browser receives a response from the server, but your alert(msg[0]) code is called immediately.

If you are seeing data in the console from your console.log(msj), this is because the console doesn't evaluate objects until you expand them. See this question. Try changing your console.log to something like one of these:

console.log(JSON.stringify(msj));
console.log(msj.length);
console.log(msj.join(","));

Edit: It sounds like you want to gather all of the responses, and then do something once you've received all of them. In your success handler, after you have added the result to msj, check the length to see if you've received all of them. When all of the responses have been received, msj.length should be the same as a.length:

success: function (result) {   
    msj.push(result);
    if (msj.length == a.length) {
        // All responses have been received. Ready to use msj!
    }
}

OTHER TIPS

Move the console and alert calls into the success function.

$.ajax({
    url:"js.php",
    cache:false,
    type:"POST",
    data:{concepto:Uno,descripcion:Dos,valor_total:Tres,valor_pagado:Cuatro,valor_f:valor_fact,saldo_pendiente:valor_recibo,numero_factura:id_fac,id_estudiante:id_student},
    success:function(result)
    {   
        msj.push(result);               
        console.log(msj);
        alert(msj[0]);        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top