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

one ajax script to check one file ,and if something change do refresh the other file!

100+
P: 155
I got an idea and i want to know if its possible!

i got a php file with ajax script that calling other php file to display it in a div ,the file size is 7 KB ,in this file there is a code that count posts and code that display the posts !

what i was thinking is instead of refreshing the file that is 7 KB every 4 second ,i want to make a little file of the counter ,that it can be 700 bytes ,and if some thing as changed it will excute the 7 KB file and display the new posts ,so if i can do that ,im saveing 90 precent of band width to the server!!!

can some help me do that?

thanx!!!
Nov 4 '08 #1
Share this Question
Share on Google+
13 Replies


gits
Expert Mod 5K+
P: 5,333
hi ... why not :) ... your callback that handles the response just need to handle it ... you could request the php-file - if something has changed it behaves as it is and in case it is unchanged just return a false or whatever ... and your callback handles it and don't need to process anything ...

kind regards
Nov 4 '08 #2

100+
P: 155
where can i find exemple for something like that?

im not expert :)

how can i use it with ifs

like if some thing change in file.php ,then continue and refresh file1.php ,if not ,do nothing!!

thanx!! :)
Nov 4 '08 #3

gits
Expert Mod 5K+
P: 5,333
i think i don't understand the problem. the typical flow is (simplified):

1. start a XMLHttpRequest
2. the serverside service processes something
3. server sends response
4. XMLHttpRequest-Object's callback function processes the response

so you just need to adapt the response that is send from the server to reflect whether something has changed or not. i guess the php-file asks a database or whatever and so it should be easy to 'echo' or whatever the 'normal' response or a false in case nothing was changed? and so the callback, the function that is assigned to your onreadystatechange property, just need to handle the response that it receives?

without seeing what you have and a further explaination what your code already does it would be guesswork to advice something ...

kind regards
Nov 4 '08 #4

100+
P: 155
here is the code i' m using right now!!
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. //Global vars to hold connection to web pages
  3.  
  4.  
  5. var request;
  6.  
  7. function getHTTPObject() 
  8. {
  9.     var xhr = false;
  10.     if (window.XMLHttpRequest) 
  11.     {
  12.         xhr = new XMLHttpRequest();
  13.     } else if (window.ActiveXObject) {
  14.         try 
  15.         {
  16.             xhr = new ActiveXObject("Msxml2.XMLHTTP");
  17.         } 
  18.         catch(e) 
  19.         {
  20.             try 
  21.             {
  22.                 xhr = new ActiveXObject("Microsoft.XMLHTTP");
  23.             } 
  24.             catch(e) 
  25.             {
  26.                 xhr = false;
  27.             }
  28.         }
  29.     }
  30.     return xhr;
  31. }
  32.  
  33. var xmlHttp
  34. var xmlHttp2
  35.  
  36. var pageno = 1;
  37.  
  38. function showPage(pageno) { 
  39.     //Function that gets called
  40.     //Currently we only call one other sub, but this could change
  41.     showStates(pageno)
  42.  
  43. }
  44.  
  45.  
  46. function showStates(pageno) { 
  47.     //This sub will populate a table with all the states and get the 
  48.     //pagination built
  49.  
  50.  
  51.     //Make the AJAX connection for both the navigation and content
  52.     xmlHttp=GetXmlHttpObject()
  53.     xmlHttp2=GetXmlHttpObject()
  54.  
  55.     //If we cant do the request error out
  56.     if (xmlHttp==null || xmlHttp2==null ) {
  57.          alert ("Browser does not support HTTP Request")
  58.          return
  59.     }
  60.  
  61.  
  62.     //First build the navigation panel
  63.     var url="getuser.php?bid_id=<?=$bid_id;?>"
  64.     url=url+"&p="+pageno
  65.     url=url+"&t=nav"
  66.     url=url+"&sid="+Math.random()
  67.  
  68.     //Once the page finished loading put it into the div
  69.     xmlHttp2.onreadystatechange=navDone 
  70.  
  71.     //Get the php page
  72.     xmlHttp2.open("GET",url,true)
  73.     xmlHttp2.send(null)
  74.  
  75.     //Build the url to call
  76.     //Pass variables through the url
  77.     var url="getuser.php?bid_id=<?=$bid_id;?>"
  78.     url=url+"&p="+pageno
  79.     url=url+"&t=con"
  80.     url=url+"&sid="+Math.random()
  81.  
  82.     //Once the page finished loading put it into the div
  83.     xmlHttp.onreadystatechange=stateChanged 
  84.  
  85.     //Get the php page
  86.     xmlHttp.open("GET",url,true)
  87.     xmlHttp.send(null)
  88.  
  89.     setTimeout('showPage(pageno)', 4000);
  90. }
  91.  
  92. function navDone() { 
  93.     //IF this is getting called when the page is done loading then fill the pagination div
  94.     if (xmlHttp2.readyState==4 ) { 
  95.          //Update the Div tag with the outputted text
  96.          document.getElementById("pgNavigation").innerHTML=xmlHttp2.responseText 
  97.     } 
  98. }
  99.  
  100. function stateChanged() { 
  101.     //IF this is getting called when the page is done loading the states then output the div
  102.     if (xmlHttp.readyState==4 ) { 
  103.          //Update the Div tag with the outputted text
  104.          document.getElementById("pgContent").innerHTML=xmlHttp.responseText 
  105.  
  106.     } 
  107. }
  108.  
  109. function GetXmlHttpObject() {
  110.     //Determine what browser we are on and make a httprequest connection for ajax
  111.     var xmlHttp=null;
  112.  
  113.     try {
  114.          // Firefox, Opera 8.0+, Safari
  115.          xmlHttp=new XMLHttpRequest();
  116.     }
  117.     catch (e) {
  118.          //Internet Explorer
  119.          try {
  120.               xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  121.           }
  122.          catch (e) {
  123.               xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  124.           }
  125.     }
  126.  
  127.     return xmlHttp;
  128. }
  129.  
  130.  
  131.  
  132. //Onload start the user off on page one
  133. window.onload = showPage(pageno);
  134.  
  135.  
  136. </script>

what i want is to have one call in the begining of the code to call some other php file that is very short in size ,and it will refresh this file every 2 seconds for example!

if there is a change in the first file ,it will continue to the end of this code!

but first i need this code to display the results! and then check the first file

thanx for your help!
Nov 4 '08 #5

gits
Expert Mod 5K+
P: 5,333
let me give you the idea again:

- add a new request that calls a script at your server
- this script (php) responds with a true or false ... depending on your requirement
- you could set an interval of 2 secs where this script is called
- now when the response is true - call the update-page method - if not, then nothing is to do
- so you just need another php-script that returns you whether you have to update the page or not ... and just another XMLHttpRequest that continuosly asks for this

with which of this suggestions you would have problems?

kind regards
Nov 4 '08 #6

100+
P: 155
the true :) i dont know how to do that ,thats why i need help

this is the way i need it ,and if someone can copy paste something here that i can learn from ,or put a code example that will be GREAT :)

i got a file now for example main.php

in this file i want to have the ajax call!!

from my point of view:

{if there is change in "file1.php" do refresh on "file2.php"

i got file with ajax that works perfect ,but i want to make the request short as i can!

pleae help!! thanx!!
Nov 4 '08 #7

100+
P: 155
Ok . i got this code that works with two divs ,now what i need to do to make the first call if it's changes change the second call!

Expand|Select|Wrap|Line Numbers
  1. var xmlhttp = false ;
  2.  
  3.  
  4. if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  5. {
  6.   try {
  7.     xmlhttp = new XMLHttpRequest ();
  8.   }
  9.   catch (e) {
  10.   xmlhttp = false}
  11. }
  12.  
  13.  
  14. function myXMLHttpRequest ()
  15. {
  16.   var xmlhttplocal;
  17.   try {
  18.       xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
  19.   catch (e) {
  20.     try {
  21.     xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")}
  22.     catch (E) {
  23.       xmlhttplocal = false;
  24.     }
  25.   }
  26.  
  27.   if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
  28.     try {
  29.       var xmlhttplocal = new XMLHttpRequest ();
  30.     }
  31.     catch (e) {
  32.       var xmlhttplocal = false;
  33.     }
  34.   }
  35.   return (xmlhttplocal);
  36. }
  37.  
  38.  
  39. var mnmxmlhttp = Array ();
  40. var mnmString = Array ();
  41. var mnmPrevColor = Array ();
  42. var responsestring = Array ();
  43. var myxmlhttp = Array ();
  44. var responseString = new String;
  45.  
  46.  
  47.  
  48.  
  49. var i=0;
  50. var ii = 0;
  51.  
  52. /// DIV1
  53. function ajax_update_div1()
  54. {
  55.  
  56.     url = "check.php";
  57.     target2 = document.getElementById ('content');
  58.  
  59.     ii = i++;
  60.  
  61.     var content = "i=" + ii ;
  62.  
  63.     mnmxmlhttp = new myXMLHttpRequest ();
  64.     if (mnmxmlhttp) {
  65.             mnmxmlhttp.open ("POST", url, true);
  66.             mnmxmlhttp.setRequestHeader ('Content-Type',
  67.                        'application/x-www-form-urlencoded');
  68.  
  69.             mnmxmlhttp.send (content);
  70.             errormatch = new RegExp ("^ERROR:");
  71.  
  72.             target2 = document.getElementById ('content');
  73.  
  74.             mnmxmlhttp.onreadystatechange = function () {
  75.                 if (mnmxmlhttp.readyState == 4) {
  76.                     mnmString = mnmxmlhttp.responseText;
  77.  
  78.                     if (mnmString.match (errormatch)) {
  79.                         mnmString = mnmString.substring (6, mnmString.length);
  80.  
  81.                         target = document.getElementById ('content');
  82.                         target2.innerHTML = mnmString;
  83.  
  84.                     } else {
  85.                         target = document.getElementById ('content');
  86.                         target2.innerHTML = mnmString;
  87.  
  88.                     }
  89.                 }
  90.             }
  91.         refrash_div1();    
  92.  
  93.         }
  94.  
  95.  
  96. }
  97.  
  98. //DIV2
  99.  
  100. function ajax_update_div2()
  101. {
  102.  
  103.     url = "st.php";
  104.     target2 = document.getElementById ('content1');
  105.  
  106.     ii = i++;
  107.  
  108.     var content = "i=" + ii ;
  109.  
  110.     mnmxmlhttp = new myXMLHttpRequest ();
  111.     if (mnmxmlhttp) {
  112.             mnmxmlhttp.open ("POST", url, true);
  113.             mnmxmlhttp.setRequestHeader ('Content-Type',
  114.                        'application/x-www-form-urlencoded');
  115.  
  116.             mnmxmlhttp.send (content);
  117.             errormatch = new RegExp ("^ERROR:");
  118.  
  119.             target2 = document.getElementById ('content1');
  120.  
  121.             mnmxmlhttp.onreadystatechange = function () {
  122.                 if (mnmxmlhttp.readyState == 4) {
  123.                     mnmString = mnmxmlhttp.responseText;
  124.  
  125.                     if (mnmString.match (errormatch)) {
  126.                         mnmString = mnmString.substring (6, mnmString.length);
  127.  
  128.                         target = document.getElementById ('content1');
  129.                         target2.innerHTML = mnmString;
  130.  
  131.                     } else {
  132.                         target = document.getElementById ('content1');
  133.                         target2.innerHTML = mnmString;
  134.  
  135.                     }
  136.                 }
  137.             }
  138.         refrash_div2();    
  139.  
  140.         }
  141.  
  142.  
  143. }
  144.  
  145. function refrash_div1() {
  146. setTimeout('ajax_update_div1()', 4000);
  147. }
  148.  
  149.  
thanx!
Nov 5 '08 #8

100+
P: 155
any on got an idea :)
Nov 5 '08 #9

gits
Expert Mod 5K+
P: 5,333
lets do it step by step. the first example you posted did work already - yes? now you just have to call a function in your document's onload that will make a new call to a php-script that just tell you whether the file or data in question would have been changed.

the function should basically look like this - assuming that check_for_update.php returns true or false - and just call this function in an interval:

Expand|Select|Wrap|Line Numbers
  1. function check_for_update() {
  2.     var request = GetXmlHttpObject();
  3.  
  4.     var url = 'check_for_update.php';
  5.     request.onreadystatechange = function(ret) {
  6.         if (request.readyState == 4) {
  7.             if (ret.responseText) {
  8.                 // do your update call
  9.             } else {
  10.                 // do nothing
  11.             }
  12.         }
  13.     } ;
  14.  
  15.     request.open('GET', url, true);
  16.     request.send(null);
  17. }
kind regards
Nov 6 '08 #10

100+
P: 155
Thanx man for helping :)

i dont know where to insert this code!

from the last code i posted where can i insert it to check the the second call?

thanx
Nov 6 '08 #11

gits
Expert Mod 5K+
P: 5,333
i'm sorry ... i have to say that coding ajax-driven webpages is no copy&paste thing ... as i said already the function i showed you has to be completed and the code you already have has to be adapted - the mentioned function just needs to be called when the page was first loaded completely ... you could add the call to the last callback of your first requests or add the call to the onload what would require that the 'check_for_update.php' script always return true when it is called for the first time - so that you could leave out the load of the page the first time and trigger it from the new function. so in case you just want someone to do the work for you ... then i'm not the right person ... and certainly i don't read a bunch of lines of code to get it to work for a completely other problem. in case you don't know what the italic-marked words would mean in our context here, then i would suggest to start with a few basic ajax-tutorials first that you may find here for example.

kind regards
Nov 6 '08 #12

100+
P: 155
Thanx man for what you allready did ,you are ok ,and i dont want you to write any thing to me ,i just ask for help!!


and you did help :)

thanx again!!!
Nov 6 '08 #13

gits
Expert Mod 5K+
P: 5,333
no problem ... in case you would post something that would show that you have implemented something that would use any suggestion that was given, then i'm more then glad to have a look over it and help fixing potential problems/bugs ...

kind regards
Nov 6 '08 #14

Post your reply

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