943,546 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1300
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 18th, 2009
0

ajax problem

Expand Post »
Hi,

I am trying to implement the following ajax code:

PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 21
Solved Threads: 0
Junior Poster
marjan_m is offline Offline
149 posts
since Apr 2005
Apr 18th, 2009
0

Re: ajax problem

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.

javascript Syntax (Toggle Plain Text)
  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. };
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Apr 18th, 2009
0

Re: ajax problem

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?
Reputation Points: 21
Solved Threads: 0
Junior Poster
marjan_m is offline Offline
149 posts
since Apr 2005
Apr 18th, 2009
0

Re: ajax problem

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
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Apr 19th, 2009
0

Re: ajax problem

Thanks for your time. I am posting the whole code, please guide about the mistakes.

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 0
Junior Poster
marjan_m is offline Offline
149 posts
since Apr 2005
Apr 19th, 2009
0

Re: ajax problem

First of all if the document is (X)Html then your link tag needs to be closed.
Click to Expand / Collapse  Quote originally posted by marjan_m ...
html Syntax (Toggle Plain Text)
  1. <link rel="stylesheet" type="text/css" href="stylesheet/style.css">
should be
html Syntax (Toggle Plain Text)
  1. <link rel="stylesheet" type="text/css" href="stylesheet/style.css" />

Secondly the script tag REQUIRES you to specify its type.
Click to Expand / Collapse  Quote originally posted by marjan_m ...
html Syntax (Toggle Plain Text)
  1. <script>
should be
html Syntax (Toggle Plain Text)
  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.

php Syntax (Toggle Plain Text)
  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>
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Apr 19th, 2009
0

Re: ajax problem

Thanks for your help. Did you get the output of this code?
Reputation Points: 21
Solved Threads: 0
Junior Poster
marjan_m is offline Offline
149 posts
since Apr 2005
Apr 19th, 2009
0

Re: ajax problem

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
Reputation Points: 21
Solved Threads: 0
Junior Poster
marjan_m is offline Offline
149 posts
since Apr 2005
Apr 19th, 2009
0

Re: ajax problem

so does it still do this with my example ?
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Apr 19th, 2009
0

Re: ajax problem

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.
Reputation Points: 21
Solved Threads: 0
Junior Poster
marjan_m is offline Offline
149 posts
since Apr 2005

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 PHP Forum Timeline: dynamic array and dymanic variables
Next Thread in PHP Forum Timeline: PHP-MySQL question





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


Follow us on Twitter


© 2011 DaniWeb® LLC