ajax problem

Thread Solved

Join Date: Apr 2005
Posts: 149
Reputation: marjan_m is an unknown quantity at this point 
Solved Threads: 0
marjan_m marjan_m is offline Offline
Junior Poster

ajax problem

 
0
  #1
Apr 18th, 2009
Hi,

I am trying to implement the following ajax code:

  1.  
  2. function create()
  3. {
  4.  
  5. obj_t = new XMLHttpRequest();
  6. if(obj_t == null)
  7. {
  8.  
  9. alert("Your broweser does not support it");
  10. }
  11.  
  12. }
  13.  
  14. function data()
  15. {
  16.  
  17. if(obj_t!=null)
  18. {
  19.  
  20. if(obj_t.readyState == 4)
  21. {
  22.  
  23. var dv = document.getElementById('tdetail');
  24. dv.innerHTML = obj_t.responseText;
  25.  
  26.  
  27. }
  28.  
  29. }
  30. }
  31.  
  32. function createRequest(id)
  33. {
  34.  
  35. obj_t.open("GET","functions/travellerdetail.php?tid="+id,true);
  36. obj_t.onreadystatechange = data;
  37. obj_t.send(null);
  38.  
  39. }
  40.  
  41.  
  42. function showdetail(tid)
  43. {
  44.  
  45. create();
  46. createRequest(tid);
  47.  
  48. }

I have called the showdetail function a link click and passed it's id. The problem is that the dv is not updated data. Is there any problem with the link?

Thanks for your time.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: ajax problem

 
0
  #2
Apr 18th, 2009
This should be in the JavaScript forum but from looking at your code quickly it seems ok but you need to make sure the browser your using supports creating your XMLHttpRequest object like you do. The following code is taken from a project of mine which I know works feel free to try it out and see if its working in place of your code.

  1. var createXmlHttpRequest = function() {
  2. var x;
  3. try {
  4. x = new XMLHttpRequest();
  5. } catch(ex) {
  6. try {
  7. x = new ActiveXObject("Msxml2.XMLHTTP");
  8. } catch(ex2) {
  9. try {
  10. x = new ActiveXObject("Microsoft.XMLHTTP");
  11. } catch(ex3) {
  12. x = false;
  13. }
  14. }
  15. }
  16. if(x) { return x; } else { return false; }
  17. };
  18.  
  19. var createUniqueUrl = function(url) {
  20. return url + (url.indexOf('?') !== -1 ? '' : '?') + '&$=' + new Date().getTime();
  21. };
  22.  
  23. var ajax = function(url, id) {
  24. var xmlHttp = createXmlHttpRequest();
  25. if(xmlHttp) {
  26. xmlHttp.onreadystatechange = function() {
  27. if(xmlHttp.readyState === 4) {
  28. var data = xmlHttp.responseText;
  29. }
  30. };
  31. url = createUniqueUrl(url + '?id=' + id);
  32. xmlHttp.open('GET', url, true);
  33. xmlHttp.send(null);
  34. }
  35. };
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 149
Reputation: marjan_m is an unknown quantity at this point 
Solved Threads: 0
marjan_m marjan_m is offline Offline
Junior Poster

Re: ajax problem

 
0
  #3
Apr 18th, 2009
Thanks for your reply.

Mine code was also working fine. I have checked by alert that it reaches on state 4 and displays data, but the data vanishes after state 4. First it shows state 2 then 3 , then 4 and then 1. Why the div is not holding the data permanently?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: ajax problem

 
0
  #4
Apr 18th, 2009
you have alerted the 'obj_t.responseText' value and it definately is returning text ? Also do you have an element in the page with ID 'tdetail'. If so can you post your pages HTML as there may be a problem elsewhere
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 149
Reputation: marjan_m is an unknown quantity at this point 
Solved Threads: 0
marjan_m marjan_m is offline Offline
Junior Poster

Re: ajax problem

 
0
  #5
Apr 19th, 2009
Thanks for your time. I am posting the whole code, please guide about the mistakes.

  1. <?
  2. include "functions/mysql.php";
  3. ?>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  9. <title>Find Travellers</title>
  10. <link rel="stylesheet" type="text/css" href="stylesheet/style.css">
  11. <script>
  12. var obj_t;
  13. function create()
  14. {
  15.  
  16. obj_t = new XMLHttpRequest();
  17. if(obj_t == null)
  18. {
  19.  
  20. alert("Your broweser does not support it");
  21. }
  22.  
  23. }
  24.  
  25. function data()
  26. {
  27.  
  28. if(obj_t!= null)
  29. {
  30.  
  31. if(obj_t.readyState == 4)
  32. {
  33.  
  34. var dv = document.getElementById('tdetail');
  35. dv.innerHTML = obj_t.responseText;
  36.  
  37.  
  38. }
  39.  
  40. }
  41. }
  42.  
  43. function createRequest(id)
  44. {
  45.  
  46.  
  47. obj_t.open("GET", "functions/travellerdetail.php?tid="+id , true);
  48.  
  49. obj_t.onreadystatechange = data;
  50. obj_t.send(null);
  51.  
  52. }
  53.  
  54.  
  55. function showdetail(tid)
  56. {
  57.  
  58. create();
  59. createRequest(tid);
  60.  
  61. }
  62. </script>
  63. </head>
  64.  
  65. <body>
  66. <table>
  67. <tr><td>
  68. <?$Q = mysql_query("select * from travellers where t_type = 'air'");?>
  69. <div id="byair">
  70. <?
  71. while($R = mysql_fetch_array($Q))
  72. {
  73. ?>
  74. <br />
  75. <a href="" class="tlink" onclick="showdetail(<? echo $R["t_id"]; ?>)" id="<? echo $R["t_id"]; ?>" name="<? echo $R["t_name"]; ?>">
  76. <? echo $R["t_name"]; ?> </a>
  77.  
  78. <? }
  79. ?>
  80. </div></td></tr>
  81. <tr><td><div id="tdetail"></div></td></tr>
  82. </table>
Last edited by marjan_m; Apr 19th, 2009 at 2:54 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: ajax problem

 
0
  #6
Apr 19th, 2009
First of all if the document is (X)Html then your link tag needs to be closed.
Originally Posted by marjan_m View Post
  1. <link rel="stylesheet" type="text/css" href="stylesheet/style.css">
should be
  1. <link rel="stylesheet" type="text/css" href="stylesheet/style.css" />

Secondly the script tag REQUIRES you to specify its type.
Originally Posted by marjan_m View Post
  1. <script>
should be
  1. <script type="text/javascript">

You also need a closing </body> and </html> tag at the end of the document. Ive taken your script and reformatted it, try the following.

  1. <?
  2. include "functions/mysql.php";
  3. ?>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <title>Find Travellers</title>
  9. <link rel="stylesheet" type="text/css" href="stylesheet/style.css" />
  10. <script language="javascript" type="text/javascript">
  11. function createXmlHttpRequest() {
  12. var x;
  13. try {
  14. x = new XMLHttpRequest();
  15. } catch(ex) {
  16. try {
  17. x = new ActiveXObject("Msxml2.XMLHTTP");
  18. } catch(ex2) {
  19. try {
  20. x = new ActiveXObject("Microsoft.XMLHTTP");
  21. } catch(ex3) {
  22. x = false;
  23. }
  24. }
  25. }
  26. if(x) { return x; } else { return false; }
  27. }
  28.  
  29. function showdetail(tid) {
  30. var xmlHttp = createXmlHttpRequest();
  31. if(xmlHttp) {
  32. xmlHttp.onreadystatechange = function() {
  33. if(xmlHttp.readyState === 4) {
  34. var data = xmlHttp.responseText;
  35. var dv = document.getElementById('tdetail');
  36. dv.innerHTML = data;
  37. }
  38. };
  39. xmlHttp.open('GET', 'functions/travellerdetail.php?tid=' + id, true);
  40. xmlHttp.send(null);
  41. }
  42. return false;
  43. }
  44. </script>
  45. </head>
  46. <body>
  47. <table>
  48. <tr>
  49. <td>
  50. <?$Q = mysql_query("select * from travellers where t_type = 'air'");?>
  51. <div id="byair">
  52. <?
  53. while($R = mysql_fetch_array($Q)) {
  54. ?>
  55. <br />
  56. <a href="#" class="tlink" onclick="return showdetail(<? echo $R["t_id"]; ?>)" id="<? echo $R["t_id"]; ?>" name="<? echo $R["t_name"]; ?>"><? echo $R["t_name"]; ?></a>
  57. <? } ?>
  58. </div>
  59. </td>
  60. </tr>
  61. <tr>
  62. <td>
  63. <div id="tdetail"></div>
  64. </td>
  65. </tr>
  66. </table>
  67. </body>
  68. </html>
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 149
Reputation: marjan_m is an unknown quantity at this point 
Solved Threads: 0
marjan_m marjan_m is offline Offline
Junior Poster

Re: ajax problem

 
0
  #7
Apr 19th, 2009
Thanks for your help. Did you get the output of this code?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 149
Reputation: marjan_m is an unknown quantity at this point 
Solved Threads: 0
marjan_m marjan_m is offline Offline
Junior Poster

Re: ajax problem

 
0
  #8
Apr 19th, 2009
The inner html of div is changed at state 4 but then vanishes at 1, why the states has sequence of 2,3,4 and then except of having the correct sequence of 1,2,3,4
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: ajax problem

 
0
  #9
Apr 19th, 2009
so does it still do this with my example ?
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 149
Reputation: marjan_m is an unknown quantity at this point 
Solved Threads: 0
marjan_m marjan_m is offline Offline
Junior Poster

Re: ajax problem

 
0
  #10
Apr 19th, 2009
I have tried your code but it is not giving the output, then I have corrected the script tag in mine code. Still the problem exists.
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 PHP Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC