0

I have the following scenario:

At onClick() event 2 javascript functions are called, each calling a AJAX POST request to a different php script. In these 2 php scripts data from 2 different databases are collected in 2 class objects. At the end of each script I'm calling a php function which checks if both php scripts ended successfully (checks a flag stored in $_SESSION). When the condition is met, I'm supposed to have 2 class objects populated with data from the 2 databases, and display them both side-by-side. At least that's what I'm trying to accomplish.

My problem is the data displayed is only for the 'last-script-to-finish'.

If you can suggest alternatives methods of how I could accomplish this, or see flaws in my logic please shout as I'm not a php or web developer at base.

TA!

2
  • How exactly do you check if both scripts has finished? Commented Nov 29, 2011 at 10:25
  • I used to modify a variable stored in $_SESSION and the check it's value. Now I'm using Niels suggestion and use JQuery's 'when().done()' approach. Commented Nov 29, 2011 at 11:33

1 Answer 1

1

you can do the following using jQuery:

$.when($.ajax("/page1.php"), $.ajax("/page2.php")).done(function(a1,  a2){
   var page1Response = a1[2].responseText;
   var page2Response = a2[2].responseText;
});

So you know both Ajax requests are ready, then you don't have to solve this serverside since your calls are clientside. More information about jQuery.when.

Sign up to request clarification or add additional context in comments.

8 Comments

sounds exactly what I need, I'll give it a try and see how it goes. TA!
You do need the jQuery library for this.
Downloaded the latest version of it and implemented your suggestion, but I clearly have a problem in the logic of it. I get the 2 responses, and I'm right back where I was before - either of those responses displays the data only from one class. When both scripts are finished both class objects should be populated though. Thanks for the suggestion though, that worked as you said it just doesn't help me achieve what I want.
But what happens if you alert(page1Response) and alert(page2Response), do you get both data right? Or is 1 of your requests not OK on server side?
They're both alright, its just when they finish I want to display both of them at the same time on the page. So I have a script which displays the data from all class objects in a table. That's the script that fails, in there the class objects are empty. I'll have to do a bit of debugging and will come back when I'll know where the problem is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.