0

I 've got 2 pages

1)index.php

2)feedy.php

1)In index.php i have this code:

   $.ajax({        type: 'GET', 

            url: 'feedy.php?n=<?echo $nnn?>',  
            data: { get_param: 'value' }, 
            success: function (data) { 
                                 var names = data


                $('#content').html(data);           
            }
        });

2)In feddy.php i have this code:

for (i=0;i<=10-1;i++){

$.ajaxSetup({async: false});
 $.post( 
             'addme.php',
             { txt: con, lnk:ln,addr:'".$addr."' },
             function(data) {
                $('#stage').html(data);

             }

}

As you can see i have on page calling another page that makes 10 calls of another page.

The problem: index.php calls feedy.php BUT it only waits to get the php output and not the ouput of the javascript code . Can i make it wait so that in feedy.php all calls can be done ?

Another solution i think could work is to find a way to send a signal to index.php that feedy has finished making calls .

Please help me beacause i can't find a solution :(

Dimitris Cal
  • 13
  • 1
  • 6

3 Answers3

1

AJAX is asynchronous, meaning that it doesn't stop the flow of execution.

When you do your AJAX call in index.php, it won't wait for the results from feedy.php because it's making an asynchronous call.

You'll have to rethink the way that feedy.php is returning data, making sure that it's doing it in a synchronous manner. An example of this would be directly echoing the data you need to the page, rather than wrapping it in an asynchronous function call.

LarryFisherman
  • 322
  • 2
  • 11
0

inside of the For loop in feddy.php, have it return a completed value to index.php, so it doesn't return until the loop is done. Or more appropriately, so that the javascript doesn't continue on index.php until the loop is done.

for (i=0;i<=10-1;i++){

    $.ajaxSetup({async: false});
    $.post( 
             'addme.php',
             { txt: con, lnk:ln,addr:'".$addr."' },
             function(data) {
                $('#stage').html(data);

             }

    if(i = 8) { //right before the i++ for the last execution
        return 'positive value';
    }
}

and then for index, add an if statement to your success

 $.ajax({        type: 'GET', 

        url: 'feedy.php?n=<?echo $nnn?>',  
        data: { get_param: 'value' }, 
        success: function (data) { 
            if(response=='positive value') {
               var names = data


              $('#content').html(data);           
           }
        }
    });
KingRichard
  • 1,214
  • 2
  • 14
  • 25
  • i did it but i didn't work. I printed a text line at the end of index.php but it didn't wait for the ajax calls ... – Dimitris Cal Dec 02 '13 at 02:16
  • This commenting restriction is killing me. I can only comment on my own answers. I'm curious about David's answer, because I use AJAX with JQuery to execute an action only if my AJAX call returns successful. – KingRichard Dec 02 '13 at 03:02
  • You may want to include an if statement within your for loop to determine if the loop is finished, then return a value to your index.php file. Once that gets returned, have the JavaScript if statement in index check the returned value and then execute. – KingRichard Dec 02 '13 at 03:03
  • How this value can be returned ? i mean the loop is never executed in feedy.php – Dimitris Cal Dec 02 '13 at 03:08
  • the problem is that the request just ignores my js code and just print php output and terminates the request. So this cannot solve the problem while i do not enter the loop in feedy ! – Dimitris Cal Dec 02 '13 at 03:35
  • Ok, I tried doing this with a few programs I have that already work, and it breaks them. As soon as the loop initiates, it's kicking back to index.php. Dejan Lukic has the right idea here. Once your first Ajax call is deemed complete, it it's all over. I'd suggest just taking the loop in feedy.php and replacing the ajax call in index.php with it. This way, the call will happen ten times and then your JS will progress. AJAX is to make someTHING happen in the background, not initiate a series of subsequent events. – KingRichard Dec 02 '13 at 05:29
  • And as far as load time goes, load your JS at the bottom of your php file. – KingRichard Dec 02 '13 at 05:30
0

You could put the JavaScript code from feddy.php into a function in index.php and call that function after your initial ajax call.

Some function like this that does everything you need to do with feddy.php...

function doFeddyCalls()
{
    for (i=0;i<=10-1;i++){
        $.ajaxSetup({async: false});
        $.post(
            'addme.php',
            { txt: con, lnk:ln, addr:'".$addr."' },
            function(data) {
                $('#stage').html(data);
        });
    }
}

And then when you make your ajax call to feddy.php, do something like this.

$.ajax({
    type: 'GET', 
    url: 'feedy.php?n=<?echo $nnn?>',  
    data: { get_param: 'value' }, 
    success: function (data)
    { 
        var names = data
        $('#content').html(data);
        doFeddyCalls();  //Ajax on feddy.php     
    }
});

Once you've used the ajax function to load feddy.php into index.php the JavaScript can act on the loaded elements.

EDIT: Since you don't want the user to see them loading, you could hide the element using $('#content').hide() or show a loading panel in the mean time. This answer describes how to do a single callback for multiple Ajax calls, which you could use to show the element once loading is complete.

Community
  • 1
  • 1
David
  • 444
  • 2
  • 9
  • But the load time of index.php is going to be very big ! I tried to do this in the past but i want to load it in the background ... – Dimitris Cal Dec 02 '13 at 02:44
  • Is there any reason you can't do it in PHP then? JavaScript within your HTML will always run on the client side, so it won't run before your server sends it. – David Dec 02 '13 at 02:58
  • feedy.php has code in js that i need. If i could write everything in php it would be great and i won't have any problem. – Dimitris Cal Dec 02 '13 at 03:09
  • Unfortunately you still can't run the JS in your HTML before you send it. You could hide the element while it loads or show a loading panel instead, and then reveal it once they are all done. Check my edit. – David Dec 02 '13 at 03:42