sending the jquery increment value to the php file with the help of the loop

advertisements

I have this for loop in jquery

$(document).ready(function() {
        for(i=0; i<counter; i++)
        {
            dataCounter = i;
            $.ajax({
              url: 'file.php',
                dataType: 'json',
                data: dataCounter,
                error: function(){
                    alert('Error loading XML document');
                },
                success: function(data){
                    $("#contents").html(data);
                }
            });
        }
});

And then I want to bring in my dataCounter into the file.php as a variable and have it change each time so I can get different records in mysql in file.php, am I doing this right? How would the php portion look like? I know how pass variables to a php file with this method if I had a form, but I don't have a get or a post form to work with. Also, my variables are going to change.

Can someone help me? Thank you!


While I don't recommend running an ajax query inside of a loop, I am wiling to explain the data option for $.ajax(). Ideally, you pass an object as the data option, and it is translated by jQuery into a query string where each object property name is a key and its value is the value:

data: {
  count: dataCounter
}

becomes

?count=1

in the query string of the ajax request if datacounter is equal to 1.

In PHP you would access it as $_GET['count'].