Hello. I have a $.post function inside of a loop. All it does it call a php function to validate an input. If the input validates, it returns "true" (which I tested and works). At this point I use a callback to do some processing and it is not working.
For example if i am looping over three items, the callback function processes the third item three times instead of each one.
Here is my relevant code:
for (step in steps) {
var step_name = steps[step];
// grab input value
var step_answer = escape($("#" + step_name).val());
if (step_answer != "") {
// check to see if answer validates
console.log(step_name) // THIS SHOWS CORRECT VALUES: 1, 2, and 3
$.post("utility.php", {
utility: "validateAnswer",
step: step_name,
answer: step_answer
},
function(data) {
// if validation suceeds..
if (data == "true") {
console.log(step_name); // THIS SHOWS WRONG VALUES: 3, 3, and 3
correct_steps.push(step_name);
}
});
}
}
Any ideas? Thanks.