By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,700 Members | 1,441 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,700 IT Pros & Developers. It's quick & easy.

AJAX , check for changes and do refresh!!

100+
P: 155
i got this php file 'check.php' that checks for changes ,and showing a counter for posts !!

Expand|Select|Wrap|Line Numbers
  1. $show_data="SELECT * FROM bidding_main WHERE bid_id='$bid_id' ";
  2. $show_data_query=mysql_query($show_data);
  3. $num_show_data=mysql_num_rows($show_data_query);
  4.         $sqlno="SELECT * FROM bidding_details WHERE bid_id=$bid_id ";
  5. $queryno=mysql_query($sqlno);
  6.         echo "<table width='517' border='0' cellspacing='0' cellpadding='0'>";
  7.         echo "    <tr>";
  8.          $rem=$max_bid-$numno;
  9.                         if($rem<=0)
  10.                         {
  11.                         echo "0";
  12.                         }
  13.                         else
  14.                         {
  15.                         echo "<td bgcolor='#FFFFFF' align='center'><font color='red'><b>הצעות לסגירה: $rem </b></font></td>";
  16.                         }"</td>";
  17.         echo "</tr>";
  18.         echo "</table>";
now, i cannot figure it out in ajax how to point the request to check if something as changed when the php file is executed?

how do i get the ajax to check that something as changed?

here is my starting ajax file

Expand|Select|Wrap|Line Numbers
  1. function loadXMLDoc(url, callBack) {
  2.     if (window.XMLHttpRequest) {
  3.         req = new XMLHttpRequest();
  4.         req.onreadystatechange = callBack;
  5.         req.open("POST", url, true);
  6.         req.send(null);
  7.     } else if (window.ActiveXObject) {
  8.         req = new ActiveXObject("Microsoft.XMLHTTP");
  9.         if (req) {
  10.             req.onreadystatechange = callBack;
  11.             req.open("POST", url, true);
  12.             req.send();
  13.         }
  14.     }
  15. }
  16.  
  17.  
  18.  
  19.  
  20.  
  21. function RefreshEngine( now)
  22. {  
  23.      var url="check.php"
  24.      if (now) url+='';
  25.  
  26.      loadXMLDoc(url, RefreshResult;
  27.  
  28. }
  29.  
  30.  
  31.  
  32. function RefreshResult ()
  33.  
  34. {
  35.  
  36.     //here im goint to have the second result
  37.  
  38. }
please help me out with this ,it's a week that im serching for result for this ,i tryed million of combinations ,and no success !!

please help or point me to the right direction!!

thanx
Nov 16 '08 #1
Share this Question
Share on Google+
12 Replies


acoder
Expert Mod 15k+
P: 16,027
Just compare the responseText with wherever the content is output, or change your PHP code to output something else when (no) changes have been made.
Nov 17 '08 #2

100+
P: 155
how can i compare the response ,this is my problem ,i don't know how the pass the value from the php file ,how to get the right value to the ajax and compare the changes?

need little more help on that!!

thanx
Nov 18 '08 #3

acoder
Expert Mod 15k+
P: 16,027
The output from the PHP file will be in req.responseText as a string. You can simply compare against whatever value you've already set:
Expand|Select|Wrap|Line Numbers
  1. if (req.responseText == "...") {
Nov 18 '08 #4

100+
P: 155
ok , im realy confused by this ajax and php thing ,i wan to make it short ,and i hope you can explain for me better!!

i got for example those two files

ajax.php

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. echo "10";
  3. ?>
and ajax.js

Expand|Select|Wrap|Line Numbers
  1. function MakeRequest()
  2. {
  3.   var xmlHttp = getXMLHttp();
  4.  
  5.   xmlHttp.onreadystatechange = function()
  6.   {
  7.     if(xmlHttp.readyState == 4)
  8.     {
  9.       HandleResponse(xmlHttp.responseText);
  10.     }
  11.   }
  12.  
  13.   xmlHttp.open("GET", "ajax.php", true); 
  14.   xmlHttp.send(null);
  15. }
  16.  
  17. function HandleResponse(response)
  18. {
  19.   document.getElementById('ResponseDiv').innerHTML = response;
  20. }
  21.  
my quastion is , after i excute this script and the response is showing me "10" ,if after i change the php file to "11" or any thing else ,does the ajax will execute another function() if it sees that there is a change in the php file?

this is how i want it to be :

ajax is checking a php file for changes ,and if is is changed execute another function!!

thanx
Nov 18 '08 #5

acoder
Expert Mod 15k+
P: 16,027
In HandleResponse(), document.getElementById('ResponseDiv').innerHTML contains the old value and response contains the new value, so compare them:
Expand|Select|Wrap|Line Numbers
  1. if (document.getElementById('ResponseDiv').innerHTML == response) {
  2.     // value hasn't changed...
  3. } else {
  4.     // value has changed...
  5. }
Nov 18 '08 #6

100+
P: 155
is this correct?

Expand|Select|Wrap|Line Numbers
  1. function MakeRequest()
  2. {
  3.   var xmlHttp = getXMLHttp();
  4.  
  5.   xmlHttp.onreadystatechange = function()
  6.   {
  7.     if(xmlHttp.readyState == 4)
  8.     {
  9.       HandleResponse(xmlHttp.responseText);
  10.     }
  11.   }
  12.  
  13.   xmlHttp.open("GET", "ajax.php", true); 
  14.   xmlHttp.send(null);
  15.  
  16.   setTimeout('MakeRequest()', 4000);
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. function HandleResponse(response)
  25. {
  26.  
  27.   if (document.getElementById('ResponseDiv').innerHTML == response) 
  28.   {
  29.  
  30.    alert ("something changed");
  31.  
  32.   } 
  33.  
  34.    else 
  35.  
  36.   {
  37.  
  38.    alert ("nothing changed");
  39.  
  40.   }
  41.  
  42.  
  43. }
Nov 18 '08 #7

acoder
Expert Mod 15k+
P: 16,027
It would be except that the Response Div must be set first in another request or during page load.
Nov 18 '08 #8

100+
P: 155
can you please please correct my wrongs, im geting crazy !!
Nov 18 '08 #9

acoder
Expert Mod 15k+
P: 16,027
What's the ResponseDiv div initially set to?
Nov 18 '08 #10

100+
P: 155
it's reading this php file


i just want it to check if the php file as changed

<?

echo "10";

?>


i want to be able to change it by hend to 11 and it will alert me that something changed!
Nov 18 '08 #11

100+
P: 155
this one is working for me heheheheeeh

Expand|Select|Wrap|Line Numbers
  1. function MakeRequest()
  2. {
  3.   var xmlHttp = getXMLHttp();
  4.  
  5.   xmlHttp.onreadystatechange = function()
  6.   {
  7.     if(xmlHttp.readyState == 4)
  8.     {
  9.       HandleResponse(xmlHttp.responseText);
  10.     }
  11.   }
  12.  
  13.   xmlHttp.open("GET", "ajax.php", true); 
  14.   xmlHttp.send(null);
  15.   setTimeout('MakeRequest()', 4000);
  16. }
  17.  
  18.  
  19.  
  20. function HandleResponse(response)
  21. {
  22.   if (document.getElementById('ResponseDiv').innerHTML == response)
  23.     {
  24.  
  25.  
  26.  
  27.     }
  28.  
  29. else if (document.getElementById('ResponseDiv').innerHTML = response)
  30.  
  31.     {
  32.  
  33.  
  34. alert("something as changed")
  35.     }
  36.  
  37. }
Nov 18 '08 #12

acoder
Expert Mod 15k+
P: 16,027
It works, but probably not in the way you intended. The line
Expand|Select|Wrap|Line Numbers
  1. else if (document.getElementById('ResponseDiv').innerHTML = response)
will always be true because you always set the innerHTML and the result is true.

A more meaningful version would be something like:
Expand|Select|Wrap|Line Numbers
  1. if (document.getElementById('ResponseDiv').innerHTML != response)
  2.     {
  3.     document.getElementById('ResponseDiv').innerHTML = response;
  4.     alert("something as changed")
  5.     }
Nov 19 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.