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!

green
  • 107
  • 1
  • 6
  • 13
  • How exactly do you check if both scripts has finished? – matino Nov 29 '11 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. – green Nov 29 '11 at 11:33

1 Answers1

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.

Niels
  • 48,601
  • 4
  • 62
  • 81
  • sounds exactly what I need, I'll give it a try and see how it goes. TA! – green Nov 29 '11 at 10:32
  • You do need the jQuery library for this. – Niels Nov 29 '11 at 10:33
  • 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. – green Nov 29 '11 at 11:09
  • 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? – Niels Nov 29 '11 at 11:13
  • 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. – green Nov 29 '11 at 11:27
  • A question more, if at the end of the php scripts I put the class objects in $_SESSION, will I be able to find them there when the php scripts end? so then I could write $.when(php1, php2).done( call a script to display all data) ? – green Nov 29 '11 at 11:46
  • Ok, the issue seems to be that what I save in $_SESSION from those php scripts called with AJAX doesn't seem to actually be saved..? – green Nov 29 '11 at 11:55
  • Ok, the issue was in the order in which I included my class files and call the session_start() as I found the fix here: http://stackoverflow.com/questions/1055728/php-session-with-an-incomplete-object. Thank you for your support! – green Nov 29 '11 at 14:14