Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    getting back the value of the variable within the success function

    I am trying to get my product id within the success function. As you can see I have a for loop that sends product_id e.g, 1,2,3...10 to "check_last_timer.php" each time it runs. Problem is how can I get back 1,2,3...10 within the success function. Each time the for loop runs, I get the last product id which is 10.

    Code:
    function check_timer(){
            //get product ids
            var product_ids= document.getElementsByClassName("product_id");
            //create for loop
            for(var i=0; i<product_ids.length; i++){
                var product_id= product_ids[i].innerHTML;
                
                $.ajax({
                    //send product id to check
                    url: "check_last_timer.php",
                    type: "post",
                    data: {product_id: product_id},
                    success: function(end_time){
                        //trying to get it here
                    }
                })
                
            }
            
        }

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,094
    Thanks
    23
    Thanked 594 Times in 593 Posts
    Because you are in a loop each return will over write the last return and you will be left with only the last results, Like you said.

    Get rid of the loop. Or run the loop on a condition return that the last results were processed and did what they were supposed do.

  • Users who have thanked sunfighter for this post:

    jaylimixx (07-04-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    But the code *CAN* work.

    For example, if you had 10 different locations to put the 10 different results, you could ensure that each one goes in the right place by using a closure as part of the function that is called upon success.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    jaylimixx (07-04-2012)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •