0

My code is supposed to pass 'seventythree' to php where it will be sent to a MYSQL database and a calculation will be made to calculate efficiency according to the values retrieved from the table seventythree. Afterwards, it's supposed to run again and get the values from seventy four. I used a for loop for this but the result just sends the same value for both the positions 0 and 1 in the array.

function calculateEfficiency(){
    for (x =0; x < 2; x++){
        var y = x+1;
        switch(y){
            case 1:
                mod = "seventythree";
                break;
            case 2:
                mod = "seventyfour";
                break;
        }
        $.ajax({
            type: 'post',
            url: 'efficiency.php',
            dataType: 'json',
            data: {'mod' : mod},
            error: function(){alert("fail");}
        }).done(function(data){
            efficiency = data.efficiency;
            refresh = data.refresh;
        }); 

        efficiencyArray[x] = efficiency;

        if (refresh){

        document.getElementById("efficiency1").innerHTML = efficiencyArray[0];
        document.getElementById("efficiency2").innerHTML = efficiencyArray[1];

        }
    }
}

What do I do?

Dave Howson
  • 156
  • 6
  • 14
  • 3
    AJAX is asynchronous. Everything that depends on the returned value has to be in the `.done()` function. The code after `$.ajax()` will be executed before you assign to the variables. – Barmar Jun 07 '17 at 17:21
  • 1
    See http://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron?newsletter=1&nlcode=97716%7c4ba7 and http://stackoverflow.com/questions/1451009/javascript-infamous-loop-issue – Barmar Jun 07 '17 at 17:22

1 Answers1

1

What @barmar is saying is that you need to place this:

efficiencyArray[x] = efficiency;

    if (refresh){

    document.getElementById("efficiency1").innerHTML = efficiencyArray[0];
    document.getElementById("efficiency2").innerHTML = efficiencyArray[1];

    }

Inside of this:

.done(function(data){
//inside here
});
JJJ
  • 3,314
  • 4
  • 29
  • 43
  • This results in the two values of the array becoming undefined. The values are passing from the php but there seems to be an error in efficiencyArray[x] = efficiency; – Dave Howson Jun 07 '17 at 17:38
  • check what the server is returning. add this inside of the `done` function and check your console: `console.log(data);` – JJJ Jun 07 '17 at 17:43
  • @DaveHowson if `efficiency` is an array, there is no need to pass it to `efficiencyArray`, you can just call it like so: `efficiency[0]` – JJJ Jun 07 '17 at 17:45
  • The values from the server pass without issue. The issue seems to be with the x inside efficiencyArray[x]. When replacing this with 0, I get the first efficiency value. – Dave Howson Jun 07 '17 at 17:45
  • get rid of the x. you are passing x as a variable when x doesnt even exist. just use `efficiency` and don't pass it to `efficiencyArray[x]`. delete that line – JJJ Jun 07 '17 at 17:47
  • @DaveHowson oh nvm so you're calling it from inside the loop and x is the number inside the loop. – JJJ Jun 07 '17 at 17:50
  • `.done(function(data){ efficiency[0] = data.efficiency; alert(x); refresh = data.refresh; if (refresh){ document.getElementById("efficiency1").innerHTML = efficiency[0]; document.getElementById("efficiency2").innerHTML = efficiency[1]; } }); ` This is how it is now but this only assigns the first efficiency, which is why I used x, so when it increases, the next efficiency value will be assigned – Dave Howson Jun 07 '17 at 17:50
  • what does `console.log(efficiencyArray);` say? place it after you create the `efficiencyArray` – JJJ Jun 07 '17 at 17:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146096/discussion-between-dave-howson-and-josan-iracheta). – Dave Howson Jun 07 '17 at 17:51