1

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);
                            }
                        });
0

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx, i've forgot to set variables global, i just had to remove the var in the each function!
@VeloFX - No problem, note that by removing var your making those variables global.
0

you have to create a loop function like this:

$ajaxsend=function(data,$arrayitems,url,total,ini){
    $.ajax({
        type: "POST",
        url: url,
        datatype: "text",
        data: data,
        success: function(data) {
            console.log(data);
            //requestCallback.requestComplete(true);
            //ini increment each loop
            ini+=1;
            //validate loop while total is > than ini
            if(total > ini){
                data = {item_id: $arrayitems[ini].item_id, item_name: $arrayitems[ini].item_name, nrlistcreatenumber: nrlistcreatenumber, nrlistcreatetime: nrlistcreatetime};
                //resend ajax call
                $ajaxsend(data,$arrayitems,url,total,ini);
            }
        }
    });
};
var $arrayitems = [];
$( ".nrlist-create_item-select" ).each(function() {
    var item_id = $( this ).attr('id');
    var item_name = $( this ).attr('title');
    //save items in array
    $arrayitems.push({"item_id":item_id,"item_name":item_name});
    //return true;
});
//initial
var ini = 0;
//total items
var total = $arrayitems.length;
//data
var data = {item_id: $arrayitems[ini].item_id, item_name: $arrayitems[ini].item_name, nrlistcreatenumber: nrlistcreatenumber, nrlistcreatetime: nrlistcreatetime};
//ajax for loop function
$ajaxsend(data,$arrayitems,'games/create-nr_vs_nr-update.php',total,ini);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.