View Full Version : getting back the value of the variable within the success function

07-03-2012, 02:23 PM
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.

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;

//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



07-03-2012, 06:20 PM
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.

Old Pedant
07-04-2012, 03:27 AM
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.