View Full Version : send array multiple times using ajax but I can't use back the same variable

06-30-2012, 05:13 PM
Emm not sure how I can describe my problem. I get the number of product ids from a class and create a for loop:

var product_ids= document.getElementsByClassName("product_id");
for(var i=0; i<product_ids.length; i++)

The I send each product id one by one using ajax

var product_id= product_ids[i].innerHTML;


url: "check_last_timer.php",
type: "post",
data: {product_id: product_id},
success: function(end_time){

Problem is within the success function I can't use the var product_id anymore, it always shows the last product id. But I really need to use it as I need to do something like this:


This is to update each individual div id. Eg, timer1, timer2 and so on. But what I got was the last product id all the time it loops. What I want is for it to start at timer1, timer2 and so on and not timer 10, timer10.... Please advice. Below is my php file:


include "connect.php";

$product_id= $_POST['product_id'];//tested at ajax

$query= "SELECT * FROM product WHERE product_id='$product_id' ";
$result= mysql_query($query);
$record= mysql_fetch_object($result);


$end_time= $record->end_time;

$end_time= strtotime($end_time); //unix_time

$now= time(); //unix time

$diff= $end_time - $now;

$min= $diff/60;
$hour= $min/60;
$hour = floor($hour);
$min %= 60;
$sec = $diff % 60;

if($hour<0 || $hour==0){
$hour= '00';

if($hour<10 && $hour>0){
$hour = '0'.$hour;

if($min<0 || $min==0){
$min= '00';

if($min<10 && $min>0){
$min = '0'.$min;

if($sec<0 || $sec==0){
$sec= '00';

if($sec<10 && $sec>0){
$sec = '0'.$sec;

echo $hour.':'.$min.':'.$sec;