AJAX , check for changes and do refresh!!

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

    AJAX , check for changes and do refresh!!

    i got this php file 'check.php' that checks for changes ,and showing a counter for posts !!

    Code:
    $show_data="SELECT * FROM bidding_main WHERE bid_id='$bid_id' ";
    $show_data_query=mysql_query($show_data);
    $num_show_data=mysql_num_rows($show_data_query);
    		$sqlno="SELECT * FROM bidding_details WHERE bid_id=$bid_id ";
    $queryno=mysql_query($sqlno);
    		echo "<table width='517' border='0' cellspacing='0' cellpadding='0'>";
    		echo "	<tr>";
    		 $rem=$max_bid-$numno;
    						if($rem<=0)
    						{
    						echo "0";
    						}
    						else
    						{
    						echo "<td bgcolor='#FFFFFF' align='center'><font color='red'><b>הצעות לסגירה: $rem </b></font></td>";
    						}"</td>";
    		echo "</tr>";
    		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

    Code:
    function loadXMLDoc(url, callBack) {
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
            req.onreadystatechange = callBack;
            req.open("POST", url, true);
            req.send(null);
        } else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                req.onreadystatechange = callBack;
                req.open("POST", url, true);
                req.send();
            }
        }
    }
    
    
    
    
    
    function RefreshEngine( now)
    {  
         var url="check.php"
         if (now) url+='';
    
         loadXMLDoc(url, RefreshResult;
         
    }
    
    
    
    function RefreshResult ()
    
    {
    	
    	//here im goint to have the second result
    	
    }
    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

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

    Comment

    • canabatz
      New Member
      • Oct 2008
      • 155

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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The output from the PHP file will be in req.responseTex t as a string. You can simply compare against whatever value you've already set:
        Code:
        if (req.responseText == "...") {

        Comment

        • canabatz
          New Member
          • Oct 2008
          • 155

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

          Code:
          <?php 
          echo "10";
          ?>
          and ajax.js

          Code:
          function MakeRequest()
          {
            var xmlHttp = getXMLHttp();
            
            xmlHttp.onreadystatechange = function()
            {
              if(xmlHttp.readyState == 4)
              {
                HandleResponse(xmlHttp.responseText);
              }
            }
          
            xmlHttp.open("GET", "ajax.php", true); 
            xmlHttp.send(null);
          }
          
          function HandleResponse(response)
          {
            document.getElementById('ResponseDiv').innerHTML = response;
          }
          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

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            In HandleResponse( ), document.getEle mentById('Respo nseDiv').innerH TML contains the old value and response contains the new value, so compare them:
            Code:
            if (document.getElementById('ResponseDiv').innerHTML == response) {
                // value hasn't changed...
            } else {
                // value has changed...
            }

            Comment

            • canabatz
              New Member
              • Oct 2008
              • 155

              #7
              is this correct?

              Code:
              function MakeRequest()
              {
                var xmlHttp = getXMLHttp();
               
                xmlHttp.onreadystatechange = function()
                {
                  if(xmlHttp.readyState == 4)
                  {
                    HandleResponse(xmlHttp.responseText);
                  }
                }
               
                xmlHttp.open("GET", "ajax.php", true); 
                xmlHttp.send(null);
                
                setTimeout('MakeRequest()', 4000);
              }
               
               
               
               
               
               
              function HandleResponse(response)
              {
              	
                if (document.getElementById('ResponseDiv').innerHTML == response) 
                {
              	  
                 alert ("something changed");
              	
                } 
              
                 else 
                 
                {
              	
                 alert ("nothing changed");
              	
                }
                
                
              }

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                It would be except that the Response Div must be set first in another request or during page load.

                Comment

                • canabatz
                  New Member
                  • Oct 2008
                  • 155

                  #9
                  can you please please correct my wrongs, im geting crazy !!

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    What's the ResponseDiv div initially set to?

                    Comment

                    • canabatz
                      New Member
                      • Oct 2008
                      • 155

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

                      Comment

                      • canabatz
                        New Member
                        • Oct 2008
                        • 155

                        #12
                        this one is working for me heheheheeeh

                        Code:
                        function MakeRequest()
                        {
                          var xmlHttp = getXMLHttp();
                          
                          xmlHttp.onreadystatechange = function()
                          {
                            if(xmlHttp.readyState == 4)
                            {
                              HandleResponse(xmlHttp.responseText);
                            }
                          }
                        
                          xmlHttp.open("GET", "ajax.php", true); 
                          xmlHttp.send(null);
                          setTimeout('MakeRequest()', 4000);
                        }
                        
                        
                        
                        function HandleResponse(response)
                        {
                          if (document.getElementById('ResponseDiv').innerHTML == response)
                            {
                          
                          
                        
                            }
                        
                        else if (document.getElementById('ResponseDiv').innerHTML = response)
                        
                            {
                           
                          
                        alert("something as changed")
                            }
                        
                        }

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          It works, but probably not in the way you intended. The line
                          Code:
                          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:
                          Code:
                          if (document.getElementById('ResponseDiv').innerHTML != response)
                              {
                              document.getElementById('ResponseDiv').innerHTML = response;
                              alert("something as changed")
                              }

                          Comment

                          Working...