Results 1 to 4 of 4

Thread: AJAX -php auto refresh time

  1. #1
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default AJAX -php auto refresh time

    hi all

    i have a time.php file in which i have code
    Code:
    echo date("H:i:s A");
    this code shows the time on the page.

    i want the ajax code that wil auto refresh the time after every 5 second. i dont want to auto refresh the whole page.

    i dont have knowledge of ajax but know that this is posible with ajax only.


    vineet

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not sure what you mean, but I decided it was that you wanted time.php imported to a page every 5 seconds. So I tried that out using my current favorite AJAX routine (http://www.dynamicdrive.com/forums/s...9&postcount=16), and it worked:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function loadXmlHttp(url, id) {
    var f = this;
    f.xmlHttp = null;
    /*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
    /*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
    try {f.ie = window.ActiveXObject}catch(e){f.ie = false;}
    @end @*/
    if (window.XMLHttpRequest&&!f.ie||/^http/.test(window.location.href))
    f.xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
    else if (/(object)|(function)/.test(typeof createRequest))
    f.xmlHttp = createRequest(); // ICEBrowser, perhaps others
    else {
    f.xmlHttp = null;
     // Internet Explorer 5 to 6, includes IE 7+ when local //
    /*@cc_on @*/
    /*@if(@_jscript_version >= 5)
    try{f.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e){try{f.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){f.xmlHttp=null;}}
    @end @*/
    }
    if(f.xmlHttp != null){
    f.el = document.getElementById(id);
    f.xmlHttp.open("GET",url,true);
    f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
    f.xmlHttp.send(null);
    }
    }
    
    
    loadXmlHttp.prototype.stateChanged=function () {
    if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !/^http/.test(window.location.href)))
    	this.el.innerHTML = this.xmlHttp.responseText;
    }
    
    var requestTime = function(){
    new loadXmlHttp('time.php', 'timeDiv');
    setInterval(function(){new loadXmlHttp('time.php?t=' + new Date().getTime(), 'timeDiv');}, 5000);
    }
    
    if (window.addEventListener)
     window.addEventListener('load', requestTime, false);
    else if (window.attachEvent)
     window.attachEvent('onload', requestTime);
    </script>
    </head>
    <body>
    <div id="timeDiv">
    
    </div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    hi john

    let me clear my self about what i want to do.

    i have time.php file in the my right colum that is included file with
    Code:
    require_once("../includes/time.php")
    in time.php i have
    Code:
    echo date("H:i:s A");
    which shows the time.

    on first visit to the page it wil show you the present time and if u r on that page for 20 minutes and if u dont refresh the page the time will be shown that is of 20minitus back.

    what i need is that the time.php file should get refresh automatically after 5 seconds so that it looks like the live clock which is showing you the present time with auto refresh server side.

    vineet

  4. #4
    Join Date
    Jun 2008
    Posts
    121
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default ajax-php time

    hi john

    i had tried it and it is working very fine as i needed like a live clock.

    thanks very much.

    vineet

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •