Rookie having problem with 2 consecutive Ajax calls: 1st message lost

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

Rookie having problem with 2 consecutive Ajax calls: 1st message lost

 
0
  #1
Aug 26th, 2009
Hello, I'm new at Ajax and Javascript. Can anyone help me on this?

I'm calling 2 Ajax funcions, one after the other, with a delay time in between.

The first call executes a script that inserts a row in a MySQL database.
The second call execustes a cript that lists what's in the database.

What I'm losing is the response from the first script saying a row has been inserted, for instance. It should appear in a div named "message".
Seems the response from the second script overrrides it, although it goes to another div named "rows".

What I'm trying now is storing the response from the first call in a variable named "reply" and then showing it in the div, along with the reply from the second call. But it's not working (shows as "undefined"), and I don't know why... any help very appreciated.

  1. var xmlhttp;
  2. var reply;
  3. function insertList(str)
  4. {
  5. insert(str);
  6. setTimeout("list()", 100);
  7. }
  8. function list()
  9. {
  10. xmlhttp=GetXmlHttpObject();
  11. if (xmlhttp==null)
  12. {
  13. alert ("Browser too old");
  14. return;
  15. }
  16. var url="list.php";
  17. url=url+"?sid="+Math.random();
  18. xmlhttp.onreadystatechange=stateChangedList;
  19. xmlhttp.open("GET",url,true);
  20. xmlhttp.send(null);
  21. }
  22. function insert(str)
  23. {
  24. xmlhttp=GetXmlHttpObject();
  25. if (xmlhttp==null)
  26. {
  27. alert ("Browser too old");
  28. return;
  29. }
  30. var url="insert.php";
  31. url=url+"?nom="+str;
  32. url=url+"&sid="+Math.random();
  33. xmlhttp.onreadystatechange=stateChangedInsert;
  34. xmlhttp.open("GET",url,true);
  35. xmlhttp.send(null);
  36. }
  37. function stateChangedLlist()
  38. {
  39. if (xmlhttp.readyState==4)
  40. {
  41. document.getElementById("rows").innerHTML=xmlhttp.responseText; <------ THIS MESSAGE SHOWS OK.
  42. document.getElementById("message").innerHTML=reply; <---- THIS IS WHAT I'M TRYING NOW BUT IT'S NOT WORKING. SHOWS AS "undefined"
  43. }
  44. }
  45. function stateChangedInsert()
  46. {
  47. if (xmlhttp.readyState==4)
  48. {
  49. document.getElementById("message").innerHTML=xmlhttp.responseText; <----- THIS MESSAGE IS LOST.
  50. reply=xmlhttp.responseText; <----- THIS IS WHAT I'M TRYING NOW BUT IT'S NOT WORKING.
  51. }
  52. }
  53. function GetXmlHttpObject()
  54. {
  55. if (window.XMLHttpRequest)
  56. { // code for IE7+, Firefox, Chrome, Opera, Safari
  57. return new XMLHttpRequest();
  58. }
  59. if (window.ActiveXObject)
  60. { // code for IE6, IE5
  61. return new ActiveXObject("Microsoft.XMLHTTP");
  62. }
  63. return null;
  64. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

Re: Rookie having problem with 2 consecutive Ajax calls: 1st message lost

 
0
  #2
Aug 27th, 2009
Anyone could give me a hint? still struggling with this.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 869
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 124
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: Rookie having problem with 2 consecutive Ajax calls: 1st message lost

 
0
  #3
Aug 27th, 2009
The simple, and conventional, solution would be to make just one AJAX call with one response containing all the info you need to display as feedback to the user (ie a confirmation message that the insertion has been made, plus the list of what's in the database).

Two calls with two responses seems to add unnecessary complexity and uncertainty.

You may need to use JSON to encode your data server-side and to decode client-side. That's what JSON does. Google "JSON" for a more detailed description/tutorial.

Javascript is easily capable of handling the two parts of the decoded response separately - eg. displaying one part in one div and the other part in another div.

Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

Re: Rookie having problem with 2 consecutive Ajax calls: 1st message lost

 
0
  #4
Aug 27th, 2009
Thanks a lot for your reply! going to try this asap.
Last edited by Altairzq; Aug 27th, 2009 at 9:29 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 869
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 124
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: Rookie having problem with 2 consecutive Ajax calls: 1st message lost

 
0
  #5
Aug 27th, 2009
Altairzq,

I think you have it!!!!

Good luck.

Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 35
Reputation: Altairzq is an unknown quantity at this point 
Solved Threads: 0
Altairzq Altairzq is offline Offline
Light Poster

Re: Rookie having problem with 2 consecutive Ajax calls: 1st message lost

 
0
  #6
Aug 27th, 2009
By George it's working!

Made a little test and i'm displaying two different messages in two divs, from a single call. The JSON functionality is just what I needed, had no idea it existed.

Thank you so much Airshow
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 869
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 124
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: Rookie having problem with 2 consecutive Ajax calls: 1st message lost

 
0
  #7
Aug 27th, 2009
Altairzq,

Well done. You have learned a lot in the last 12 hours. It should serve you well in the future.

Airshow
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC