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 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    Emm not sure how I can describe my problem. I get the number of product ids from a class and create a for loop:

    Code:
    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

    Code:
    var product_id= product_ids[i].innerHTML;
                
                $.ajax({
                    
                    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:

    $("#timer"+product_id).html(end_time);

    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:

    PHP Code:
    <?php

        
    include "connect.php";
        
        
    $product_id$_POST['product_id'];//tested at ajax
        
        
        
    $query"SELECT * FROM product WHERE product_id='$product_id' ";
        
    $resultmysql_query($query);
        
    $recordmysql_fetch_object($result);
        
        
    date_default_timezone_set('Asia/Singapore');
        
        
    $end_time$record->end_time;
         
        
    $end_timestrtotime($end_time); //unix_time
            
        
    $nowtime(); //unix time
        
        
    $diff$end_time $now;
            
        
    $min$diff/60;
        
    $hour$min/60;
        
    $hour floor($hour);
        
    $min %= 60;
        
    $sec $diff 60;

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

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

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

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

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

        if(
    $sec<10 && $sec>0){
            
    $sec '0'.$sec;
        }
        
        
        
        echo 
    $hour.':'.$min.':'.$sec;
        
    ?>
    Last edited by jaylimixx; 06-30-2012 at 05:17 PM.


 

Posting Permissions

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