VeloFX VeloFX - 5 months ago 38
jQuery Question

multiple ajax php requests with each loop

I've got this code:
It comes after some if querys and this works perfectly.
The first ajax request works also fine but after the each loop the second request wont be executed.
Idk why, hopefully someone have a solution.

alert("success"); // Everything is OK
$( ".nrlist-create_item-select" ).each(function() {
var item_id = $( this ).attr('id');
var item_name = $( this ).attr('title');

$.ajax({
type: "POST",
url: "games/create-nr_vs_nr-update.php",
datatype: "text",
data: {item_id: item_id, item_name: item_name, nrlistcreatenumber: nrlistcreatenumber, nrlistcreatetime: nrlistcreatetime},
success: function(data) {
alert(data);
requestCallback.requestComplete(true);
}
});

return true;
});

$.ajax({
type: "POST",
url: "games/create-nr_vs_nr-insert.php",
datatype: "text",
data: {item_id: item_id, item_name: item_name, nrlistcreatenumber: nrlistcreatenumber, nrlistcreatetime: nrlistcreatetime},
success: function(data) {
alert(data);
requestCallback.requestComplete(true);
}
});

Answer
var item_id;
var item_name;

These variable do not seem to be defined for the second ajax call. And remember that javascript is asynchronous, it will not wait for your first ajax call to be complete before it calls the second one. If you need this to happen, take a look a jquery deffered objects.

Comments