开发者

Jquery ajax post function

开发者 https://www.devze.com 2023-04-03 02:34 出处:网络
Hi guys i am retrieving data from php using ajax , what i am trying to do is first i am finding the list of online users and then based on that list i am making request to the server let say if 2 peop

Hi guys i am retrieving data from php using ajax , what i am trying to do is first i am finding the list of online users and then based on that list i am making request to the server let say if 2 people are logged in then i am using for loop to find the numbers and then getting data for only those 2 users here's my JS code . #name is equal to logged users name , if one user is logged in #name for that user will be 1 for other it will be 2 and so on

    $(function(){
    function liveRefresh(){
    var count = 1;

    for(x=0; x<=count; x++)
       {
    var track = $('#name' + x).val();       

    alert(track);   

    parameters = 'send_to=' + track;

        $.ajax({                
            url: "scripts/live-refresh.php", 
            type: "POST",
            data: parameters,     
            cache: false,
            success: function(html){

            alert(track);
        }
        });
    }
}

liveRefresh();

});

if i alert track variable before the ajax request it shows me all the nam开发者_开发技巧es but after making ajax when i am alerting the same variable it says undefined.

You can try this code to see what's happening and see if there is any way around

Thank You


$(function(){
    function liveRefresh(){
    var count = 1;

    for(var x = 0 ; x <= count; x++)
    {
        var track = $('#name' + x).val();       
        do_ajax( parameters, track );
    }
}

function do_ajax( parameters, track ) 
{        
        $.ajax({                
            url: "scripts/live-refresh.php", 
            type: "POST",
            data: parameters,     
            cache: false,
            success: function(html){
            alert(track);
            }
        });
}

liveRefresh();

You need to form a closure to preserve the value.


@3nigma to change a variable name wont help or needed, and making it global "can" help but it will be overwritten as soon as you goto the next in the loop.

@Shanon: Im not to sure this is the best approach to do this kind of a thing. I would rather update the entire list then making a number of AJAX calls to the server.

but if you want to run it like that, then this should work:

$(function(){
    var callAjax = function(id) {
        var parameters = 'send_to=' + id;

        $.ajax({
            url: "scripts/live-refresh.php", 
            type: "POST",
            data: parameters,     
            cache: false,
            success: function(html){
                alert(id);
            }
        });
    },
    liveRefresh = function() {
        var count = 1;
        for(var x=0;x<=count;x++) {
            var track = $('#name' + x).val();       

            callAjax(track);
        }

    };

    liveRefresh();

});
0

精彩评论

暂无评论...
验证码 换一张
取 消