Danish
Danish

Reputation: 115

Php Ajax - Multiple calls in one page

Right now I have this code loading only 1 page (load.php?cid=1 but I want to load 5-8 (cid=1,cid=2,cid=3,cid=4,cid=5,cid=6,cid=10 ...etc )in different div(s).

how will I achieve it ?

$(document).ready(function() {
        function loading_show() {
            $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        }

        function loading_hide() {
            $('#loading_Paging').fadeOut 'fast');
        }

        function loadData(page) {
            loading_show();
            $.ajax({
                type: "POST",
                url: "load.php?cid=1",
                data: "page=" + page,
                success: function(msg) {
                    $("#container_Paging").ajaxComplete(function(event, request, settings) {
                        loading_hide();
                        $("#container_Paging").html(msg);
                    });
                }
            });
        }

Upvotes: 2

Views: 571

Answers (1)

Wesley Smith
Wesley Smith

Reputation: 19571

As JimL alluded to, I would give each element on your page a common class, give each element a unique data attribute like data-cid="1", iterate through each element grabbing the cids and calling the ajax function for each.

Id go a step further by using a promise to get all of the ajax responses then load all the data when the promise has been resolved (when all the ajax requests have been completed).

Here is a working example

The HTML:

<div id="loading_Paging"></div>
<div class="myElements" data-cid="1"></div>
<div class="myElements" data-cid="2" ></div>
<div class="myElements" data-cid="3" ></div>
<div class="myElements" data-cid="4" ></div>
<div class="myElements" data-cid="5" ></div>
<div class="myElements" data-cid="6" ></div>
<div class="myElements" data-cid="7"></div>
<div class="myElements" data-cid="8" ></div>

The jQuery:

$(function() {

    var page = 'some string...'
    loadData(page);

    function loadData(){
        $('#loading_Paging').html("<img src='images/loading.gif'/>").fadeIn('fast');
        // loop through each image element
        // calling the ajax function for each and storing the reponses in a `promise`
        var promises = $('.myElements').map(function(index, element) {
            var cid = '&&cid=' + $(this).data('cid'); // get the cid attribute 
            return $.ajax({
                    type: "POST",
                    url: 'load.php',
                    data: "page=" + page +cid, //add the cid info to the post data
                    success: function(msg) {
                    }
            });
        });
        // once all of the ajax calls have returned, te promise is resolved 
        // and the below function is called 
        $.when.apply($, promises).then(function() {
            // arguments[0][0] is first result
            // arguments[1][0] is second result and so on
            for (var i = 0; i < arguments.length; i++) {
                $('.myElements').eq(i).html( arguments[i][0] );
            }
            $('#loading_Paging').fadeOut('fast');
        });
     }
});

The PHP I used for my example:

<?php
if (isset($_POST['cid']) ){
    // this is just a contrived example
    // in your code youd use the cid to 
    // get whatever data you need for the current div
    echo 'This is returned message '.$_POST['cid'];
}
?>

Upvotes: 2

Related Questions