1

I'm fetching info from the database (PHP/MySQL), and send the result (via AJAX) into a JSON object. I'm collecting the result into an array. When I try to display the content of the array in some inputs, I have an "undefined" result. But when I console my array, everything is here! I'm not sure I understand the process... Would somebody please help ? (I hope I'm not repeating another question I didn't find...) THANK YOU!

 function exportData(dbname, line) {
    var tab = [];
    var content;
    $.ajax({
        type: "post",
        url: "myPHPfile.php",
        dataType: "json",
        data: { dbname: dbname, line: line },
        success: function(data) {
            for (var i=0; i < data.length-1; i++) {
                data.forEach(function (a) {
                    Object.keys(a).forEach(function(key) {
                        a[key] = Number(a[key]) || a[key];
                    });
                });
                tab[i] = data[line][i];
            }
        },
        error: function (req, stat, err) {
            console.log("Reading PHP Error: " + req.responseText);
        }
    });


    for (var k=0; k < tab.length; k++) {
        content += "<input type='text' name='" + tab[k] + "' value='" + tab[k] + "'/>";
        $("#mywindow").append(content);
    }

    console.log(tab);   //Gives a nice result of the content of the array
    console.log(tab[0]);    //Gives "undefined"!
}
EricF
  • 181
  • 1
  • 3
  • 19
  • Ajax is async.. – Eddie Feb 09 '18 at 14:11
  • @Kleioz, no I have a content in the whole array (when console.log(tab)). – EricF Feb 09 '18 at 14:13
  • 1
    Edited : Eddie is right. Ajax is async. If tab is properly shown, this is because a reference to "tab" already exists before the console.log. Nevertheless, tab[0] is undefined because no reference exists yet. – Kleioz Feb 09 '18 at 14:14
  • I strongly suspect there's a mistake in your debugging somewhere. The linked duplicate certainly addresses the overall problem of trying to use a response before it's been received. But the assertion of being able to print the entire array and then not being able to print an element of the array *must* be false. Maybe this isn't the exact code you're using, maybe the first *element* of the defined array is undefined, maybe `tab` isn't actually an array when you print it, maybe something else, but *something* is definitely incorrect about your debugging here. – David Feb 09 '18 at 14:16
  • @EricF Try to move those `console.log` lines into the `success` callback function of the ajax call – Alon Eitan Feb 09 '18 at 14:16
  • @Alon Eitan, I did that, with no success... :( – EricF Feb 09 '18 at 14:24
  • @EricF: Indicate specifically what you "did" by updating the code in the question and define specifically your criteria for "success". At its simplest, in order to use information obtained from an asynchronous operation (such as an AJAX call), you need to use it in the callback for that operation. The code in the question is simply trying to use data that doesn't exist yet at that time. If you claim that you are using the data in the callback and still encountering a problem, we'll need that code and the details of that problem. – David Feb 09 '18 at 14:29
  • @David, thanks. I guess it's something in my debugging. The real code is very similar to what you have here, except that I have a more evolved content to append. Rest of it is the same. Thanks guys, I'll dig more into my code ! :) – EricF Feb 09 '18 at 14:30
  • @EricF OK, so it's pretty much a debugging effort, because it doesn't make much sense - Are you able to see the inputs being appended into the `#mywindow` element? There must be something we/you are missing here – Alon Eitan Feb 09 '18 at 14:31
  • Yes @Alon Eitan, I see stuff. "undefined" stuff, but I see all of it. – EricF Feb 09 '18 at 14:34
  • @EricF So first of all, you need to move that logic the the ajax `success` function (That's the main issue that you must address first, and that's why I flagged your post as duplicate). After you move the code that append the elements to the DOM and the `console.log` lines, you should `console.log(data[line][i])` and make sure you're referencing to the correct properties - If you'll see `undefined` there, then it would be a good indication that something is wrong in the `tab[i] = data[line][i];` line – Alon Eitan Feb 09 '18 at 14:39
  • 1
    @Alon Eitan, you're right! When I move the logic into the `success` function, it works properly! I now understand what means `asynchronous`!! THANK YOU! – EricF Feb 09 '18 at 14:47

0 Answers0