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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • canabatz
    New Member
    • Oct 2008
    • 155

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

    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!!!
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    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

    Comment

    • canabatz
      New Member
      • Oct 2008
      • 155

      #3
      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!! :)

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        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 onreadystatecha nge 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

        Comment

        • canabatz
          New Member
          • Oct 2008
          • 155

          #5
          here is the code i' m using right now!!
          Code:
          <script type="text/javascript">
          //Global vars to hold connection to web pages
          
          
          var request;
          
          function getHTTPObject() 
          {
              var xhr = false;
              if (window.XMLHttpRequest) 
              {
                  xhr = new XMLHttpRequest();
              } else if (window.ActiveXObject) {
                  try 
                  {
                      xhr = new ActiveXObject("Msxml2.XMLHTTP");
                  } 
                  catch(e) 
                  {
                      try 
                      {
                          xhr = new ActiveXObject("Microsoft.XMLHTTP");
                      } 
                      catch(e) 
                      {
                          xhr = false;
                      }
                  }
              }
              return xhr;
          }
          
          var xmlHttp
          var xmlHttp2
          
          var pageno = 1;
          
          function showPage(pageno) { 
          	//Function that gets called
          	//Currently we only call one other sub, but this could change
          	showStates(pageno)
          		
          }
          
          
          function showStates(pageno) { 
          	//This sub will populate a table with all the states and get the 
          	//pagination built
          	
          
          	//Make the AJAX connection for both the navigation and content
          	xmlHttp=GetXmlHttpObject()
          	xmlHttp2=GetXmlHttpObject()
          	
          	//If we cant do the request error out
          	if (xmlHttp==null || xmlHttp2==null ) {
          	 	alert ("Browser does not support HTTP Request")
          	 	return
          	}
          	
          
          	//First build the navigation panel
          	var url="getuser.php?bid_id=<?=$bid_id;?>"
          	url=url+"&p="+pageno
          	url=url+"&t=nav"
          	url=url+"&sid="+Math.random()
          
          	//Once the page finished loading put it into the div
          	xmlHttp2.onreadystatechange=navDone 
          
          	//Get the php page
          	xmlHttp2.open("GET",url,true)
          	xmlHttp2.send(null)
          	
          	//Build the url to call
          	//Pass variables through the url
          	var url="getuser.php?bid_id=<?=$bid_id;?>"
          	url=url+"&p="+pageno
          	url=url+"&t=con"
          	url=url+"&sid="+Math.random()
          	
          	//Once the page finished loading put it into the div
          	xmlHttp.onreadystatechange=stateChanged 
          	
          	//Get the php page
          	xmlHttp.open("GET",url,true)
          	xmlHttp.send(null)
          
          	setTimeout('showPage(pageno)', 4000);
          }
          
          function navDone() { 
          	//IF this is getting called when the page is done loading then fill the pagination div
          	if (xmlHttp2.readyState==4 ) { 
          	 	//Update the Div tag with the outputted text
          	 	document.getElementById("pgNavigation").innerHTML=xmlHttp2.responseText 
          	} 
          }
          
          function stateChanged() { 
          	//IF this is getting called when the page is done loading the states then output the div
          	if (xmlHttp.readyState==4 ) { 
          	 	//Update the Div tag with the outputted text
          	 	document.getElementById("pgContent").innerHTML=xmlHttp.responseText 
          		
          	} 
          }
          
          function GetXmlHttpObject() {
          	//Determine what browser we are on and make a httprequest connection for ajax
          	var xmlHttp=null;
          
          	try {
          	 	// Firefox, Opera 8.0+, Safari
          	 	xmlHttp=new XMLHttpRequest();
          	}
          	catch (e) {
          	 	//Internet Explorer
          	 	try {
          	  		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          	  	}
          	 	catch (e) {
          	  		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          	  	}
          	}
          	
          	return xmlHttp;
          }
          
          
          
          //Onload start the user off on page one
          window.onload = showPage(pageno);
          
          
          </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!

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            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

            Comment

            • canabatz
              New Member
              • Oct 2008
              • 155

              #7
              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!!

              Comment

              • canabatz
                New Member
                • Oct 2008
                • 155

                #8
                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!

                Code:
                var xmlhttp = false ;
                
                
                if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
                {
                  try {
                	xmlhttp = new XMLHttpRequest ();
                  }
                  catch (e) {
                  xmlhttp = false}
                }
                
                
                function myXMLHttpRequest ()
                {
                  var xmlhttplocal;
                  try {
                  	xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
                  catch (e) {
                	try {
                	xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")}
                	catch (E) {
                	  xmlhttplocal = false;
                	}
                  }
                
                  if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
                	try {
                	  var xmlhttplocal = new XMLHttpRequest ();
                	}
                	catch (e) {
                	  var xmlhttplocal = false;
                	}
                  }
                  return (xmlhttplocal);
                }
                
                
                var mnmxmlhttp = Array ();
                var mnmString = Array ();
                var mnmPrevColor = Array ();
                var responsestring = Array ();
                var myxmlhttp = Array ();
                var responseString = new String;
                
                
                
                
                var i=0;
                var ii = 0;
                
                /// DIV1
                function ajax_update_div1()
                {
                
                	url = "check.php";
                	target2 = document.getElementById ('content');
                	
                	ii = i++;
                
                	var content = "i=" + ii ;
                
                	mnmxmlhttp = new myXMLHttpRequest ();
                	if (mnmxmlhttp) {
                			mnmxmlhttp.open ("POST", url, true);
                			mnmxmlhttp.setRequestHeader ('Content-Type',
                					   'application/x-www-form-urlencoded');
                
                			mnmxmlhttp.send (content);
                			errormatch = new RegExp ("^ERROR:");
                
                			target2 = document.getElementById ('content');
                
                			mnmxmlhttp.onreadystatechange = function () {
                				if (mnmxmlhttp.readyState == 4) {
                					mnmString = mnmxmlhttp.responseText;
                			
                					if (mnmString.match (errormatch)) {
                						mnmString = mnmString.substring (6, mnmString.length);
                						
                						target = document.getElementById ('content');
                						target2.innerHTML = mnmString;
                						
                					} else {
                						target = document.getElementById ('content');
                						target2.innerHTML = mnmString;
                
                					}
                				}
                			}
                		refrash_div1();	
                			
                		}
                
                
                }
                
                //DIV2
                
                function ajax_update_div2()
                {
                
                	url = "st.php";
                	target2 = document.getElementById ('content1');
                	
                	ii = i++;
                
                	var content = "i=" + ii ;
                
                	mnmxmlhttp = new myXMLHttpRequest ();
                	if (mnmxmlhttp) {
                			mnmxmlhttp.open ("POST", url, true);
                			mnmxmlhttp.setRequestHeader ('Content-Type',
                					   'application/x-www-form-urlencoded');
                
                			mnmxmlhttp.send (content);
                			errormatch = new RegExp ("^ERROR:");
                
                			target2 = document.getElementById ('content1');
                
                			mnmxmlhttp.onreadystatechange = function () {
                				if (mnmxmlhttp.readyState == 4) {
                					mnmString = mnmxmlhttp.responseText;
                			
                					if (mnmString.match (errormatch)) {
                						mnmString = mnmString.substring (6, mnmString.length);
                						
                						target = document.getElementById ('content1');
                						target2.innerHTML = mnmString;
                						
                					} else {
                						target = document.getElementById ('content1');
                						target2.innerHTML = mnmString;
                
                					}
                				}
                			}
                		refrash_div2();	
                			
                		}
                
                
                }
                
                function refrash_div1() {
                setTimeout('ajax_update_div1()', 4000);
                }
                thanx!

                Comment

                • canabatz
                  New Member
                  • Oct 2008
                  • 155

                  #9
                  any on got an idea :)

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    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_updat e.php returns true or false - and just call this function in an interval:

                    [CODE=javascript]function check_for_updat e() {
                    var request = GetXmlHttpObjec t();

                    var url = 'check_for_upda te.php';
                    request.onready statechange = function(ret) {
                    if (request.readyS tate == 4) {
                    if (ret.responseTe xt) {
                    // do your update call
                    } else {
                    // do nothing
                    }
                    }
                    } ;

                    request.open('G ET', url, true);
                    request.send(nu ll);
                    }[/CODE]
                    kind regards

                    Comment

                    • canabatz
                      New Member
                      • Oct 2008
                      • 155

                      #11
                      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

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5388

                        #12
                        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_upda te.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

                        Comment

                        • canabatz
                          New Member
                          • Oct 2008
                          • 155

                          #13
                          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!!!

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5388

                            #14
                            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

                            Comment

                            Working...