How can i get part of my sites content to refresh without the whole page refreshing?

Discussion in 'PHP' started by articledirectory, Sep 8, 2009.

  1. #1
    I have a section of my sites header that has some information in it that come from the mysql database, i run a cron job every 5 mins so the content gets updated, i then want just that section in the header to be refreshed without refreshing the whole page, i have been told this can only be done using ajax, but are there any other methods to get this done?
     
    articledirectory, Sep 8, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No, just ajax (or flash). Can I ask, every 5 minutes seems really slow. Are people staying that long on the page?
     
    premiumscripts, Sep 8, 2009 IP
  3. articledirectory

    articledirectory Peon

    Messages:
    1,704
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The information is displayed thoughout the whole site, in the header, time spent on the site on average is 12-15 mins per user.

    I didnt know if something could be done using Iframe, the header page would include the iframe, the iframe would display the data, then the iframe page would refresh rather than the whole page.

    So there is definatly no work around? it has to be ajax or flash then?
     
    articledirectory, Sep 8, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well yeah, you could do it via an iframe. Just place this in the iframed page (in the <head> section):

    <meta http-equiv="refresh" content="300" />
    HTML:
    (60s x 5)
     
    premiumscripts, Sep 8, 2009 IP
    articledirectory likes this.
  5. articledirectory

    articledirectory Peon

    Messages:
    1,704
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks
    i have given you +REP
    can you tell me would that code refresh the iframe/page every 5 mins from when the page is loaded, or can you get the page to refresh every 5 mins from the server time so it would coincide with the cron update?
     
    articledirectory, Sep 8, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It's from when the page is loaded.. I suppose you could do it via javascript as well but that would depend on the user clock being set at the same time.. which usually isn't the case. (to be able to reload at 5:00, 5:05 etc)

    You could however write a PHP script in that iframe.. that checks the current time and calculates how many seconds until the next refresh/cron. Then just add 10 seconds to be sure the cronjob is finished.

    The php script will simply write a javascript variable, then you'd use javascript to count down that nr of seconds until refresh.

    All this is conceptual, if someone wants to they can write it in code. Or find a better way.
     
    premiumscripts, Sep 8, 2009 IP
  7. yuvrajm

    yuvrajm Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Its better if you simply opt for a javascript library with Ajax support, and using function setInterval(); to call the ajax function to re-load the contents.
    I am more in JQuery, so for that, the script shall be :
    
    function RefreshDIV(){
    $('#div').load('page.html');
    }
    setInterval("RefreshDIV()", 3000):
    
    Code (markup):
    you may use any other library or simply an ajax custom function like:

    
    <script type="text/javascript">
    var xmlhttp;
    function loadXMLDoc(url)
    {
    xmlhttp=null;
    if (window.XMLHttpRequest)
      {// code for Firefox, Opera, IE7, etc.
      xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    if (xmlhttp!=null)
      {
      xmlhttp.onreadystatechange=state_Change;
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
      }
    else
      {
      alert("Your browser does not support XMLHTTP.");
      }
    }
    
    function state_Change()
    {
    if (xmlhttp.readyState==4)
      {// 4 = "loaded"
      if (xmlhttp.status==200)
        {// 200 = "OK"
        document.getElementById('T1').innerHTML=xmlhttp.responseText;
        }
      else
        {
        alert("Problem retrieving data:" + xmlhttp.statusText);
        }
      }
    }
    </script>
    
    Code (markup):
    you may get a better hold of Ajax on:
    http://www.w3schools.com/Ajax/default.asp
    Code (markup):
     
    yuvrajm, Sep 8, 2009 IP