943,861 Members | Top Members by Rank

Ad:
Aug 26th, 2009
0

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

Expand Post »
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.

javascript Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Altairzq is offline Offline
52 posts
since Aug 2009
Aug 27th, 2009
0

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

Anyone could give me a hint? still struggling with this.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Altairzq is offline Offline
52 posts
since Aug 2009
Aug 27th, 2009
0

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

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
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
Aug 27th, 2009
0

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

Thanks a lot for your reply! going to try this asap.
Last edited by Altairzq; Aug 27th, 2009 at 9:29 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Altairzq is offline Offline
52 posts
since Aug 2009
Aug 27th, 2009
0

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

Altairzq,

I think you have it!!!!

Good luck.

Airshow
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009
Aug 27th, 2009
0

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

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Altairzq is offline Offline
52 posts
since Aug 2009
Aug 27th, 2009
0

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

Altairzq,

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

Airshow
Sponsor
Reputation Points: 318
Solved Threads: 358
WiFi Lounge Lizard
Airshow is offline Offline
2,526 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Generated Table Rows
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Issues with javascript in IE6





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC