2

I am trying to push a div id into an array.Array push is working good bofore ajax call.But when i use push inside ajax success first push is taken place when i click on the second element.that is

array operation when with below code( array push inside success)

first click on id="1"  --- resuting array []
second click on id="2"  --- resulting array [1]
second click on id="3"  --- resulting array [1,2]

my code

$(document).ready(function() {

    var count = 0;
    var vPool = '';
    arr = [];
    seat = [];
    var totalseat = '<?php echo $sumofseat; ?>';
    var date = ' <?php echo $new_date; ?>';
    $('.custom_checkbox').click(function() {
        pressed = true;
        var prev = $(this).attr('class');
        var arrid = $(this).attr('id');
        var seats = $(this).attr('title');
        count = $('.selected').length;
        if (prev == 'custom_checkbox') {

            //arr.push(arrid);
            //seat.push(seats);
            $.ajax({
                url: "seat_manipulation.php",
                dataType: 'json',
                data: '&operation=save&seat=' + arrid + '&guid=<?php echo $guid; ?>&date=' + date,
                type: "POST",
                context: this,
                success: function(data) {

                    if (data.status == 'SAVED') {
                        $(this).toggleClass('selected');
                        $('#count').slideDown();
                        $('#selecte_seat').show();
                        $('#count').html(count + ' Seats selected');
                        alert(arrid);
                        //if(jQuery.inArray(arrid,arr) == -1) {
                        arr.push(arrid);


                        //}
                        //if(jQuery.inArray(seats,seat) == -1) {
                        seat.push(seats);
                        //}
                    } else {
                        alert("Seat already been used.Please select another");
                    }

                }
            })

        }
    });
});

am i wrong..or this is how its suposed to work ?? Thanks in advance

1
  • The more pertinent information would be where you use that array ? Commented Sep 7, 2012 at 9:28

2 Answers 2

6

You need to configure you're Ajax with "async:false" ,because there is a Race Condition thing ,so block the code while you're manipulating Array's.

Se this question.

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

2 Comments

This is exactly what I needed - if you're using the response OUTSIDE of the success ajax function, you need async: false.
This worked for me as well. I had a var set outside scope of the ajax call success method and was not able to .push a value from inside to the outside variable. Once I set this to "async:false", it worked.
3

The AJAX call you are making is asynchronous (by definition...), meaning that the actual function you are defining in $('.custom_checkbox').click is already finished before the success function is called... When you click on the next div (e.g. div 2) then the success function of the first click may or may not have already been called...

Could this be the problem?

1 Comment

Well, right now you are alerting before the push, so it makes complete sense that the current "push" has not yet taken place..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.