The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 9 of 9

Thread: AJAX and PHP Auto Refresh

  1. #1

    AJAX and PHP Auto Refresh

    Hi,

    I am currently working on a site where I'd like a portion of the page to auto refresh every 15-20 seconds. I have a PHP page that has four includes from other pages and a query from a database. I'd like one of the includes to auto refresh as this is calling a select from a complete database table at random with a maximum of 5 results, so I'd like those results to change every 15-20 seconds without refreshing the whole page.

    I looked at a meta refresh on the include page but that just refreshed the whole page when I just want the include portion to refresh. I Googled it and saw something about AJAX being able to do this. I looked at a couple of scripts but they were working with ASP and I haven't got a clue about ASP.

    Can anyone give me any pointers where I can find a tutorial about integrating this into a PHP page or help me out a little?

    Thanks

  2. #2
    Hi all, just an update to see if anyone can see the problems for IE7 with this code that I found in another forum. The code works brilliant on Firefox, but as soon as IE7 encounters it it throws up an error c00ce56e. After looking around I found a lot of people have had this problem and it relates to the page encoding. But after trying out a few of their suggestions for solving it I'm still stumped.

    Here is the code:
    <script language="javascript">


    function createRequestObject() {

    var req;

    if(window.XMLHttpRequest){
    // Firefox, Safari, Opera...
    req = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
    // Internet Explorer 5+
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
    // There is an error creating the object,
    // just as an old browser is being used.
    alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
    }

    return req;

    }

    // Make the XMLHttpRequest object
    var http = createRequestObject();

    function sendRequest(page) {

    // Open PHP script for requests
    http.open('get', page);
    http.onreadystatechange = handleResponse;
    http.send(null);

    }

    function handleResponse() {

    if(http.readyState == 4 && http.status == 200){

    // Text returned FROM the PHP script
    var response = http.responseText;

    if(response) {
    // UPDATE ajaxTest content
    document.getElementById("msgstatus").innerHTML = response;
    }

    }

    }

    function repeatloop()
    {
    sendRequest('featured.php'); // replace "inbox-status.php" with your php page's url
    setTimeout("repeatloop()", 20000);
    }

    window.onload=function() {
    repeatloop();
    }
    </script>

    In Firefox this refreshes the selected part of the page but in IE7 it does nothing except tell me there is a problem. Who would have ever thought IE7 wouldn't work properly with something?

    Can anyone see where I can fix this to work with IE7 as well as Firefox?

  3. #3
    An easier way would be to use jQuery I think. You can do a lot of javascript things in jquery, and have the code be a lot less confusing, and easier to understand. Idk if this does it better or not, but you might look into jQuery too... lol

    Just and idea... peace

  4. #4
    the function create request object seams to be incomplete:

    Code:
       GetXmlHttpObject = function()
       {
           var XMLHttp = null;
       //  active x internetexplorer ---------------------------------------------------------
          try
          {
               XMLHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); // ie7
          }
          catch (e)
          {
             try
             {
                   XMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //ie 6
             }
             catch(e)
             {
                try
                {
                   XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");//ie older versions
                }
                catch(e)
                {
                }
             }
          }
       //  active x internetexplorer ---------------------------------------------------------
    
             if (XMLHttp == null)
             {
                XMLHttp = new XMLHttpRequest();
             }
             return XMLHttp;
       }

  5. #5
    Hi borrob,

    That script has returned a different error -1072896658 on line 7, which is this one:

    var response = http.responseText;

    Here is the updated code:
    <SCRIPT language=JavaScript type="text/javascript">


    GetXmlHttpObject = function()
    {
    var XMLHttp = null;
    // active x internetexplorer ---------------------------------------------------------
    try
    {
    XMLHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); // ie7
    }
    catch (e)
    {
    try
    {
    XMLHttp = new ActiveXObject("Msxml2.XMLHTTP"); //ie 6
    }
    catch(e)
    {
    try
    {
    XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");//ie older versions
    }
    catch(e)
    {
    }
    }
    }
    // active x internetexplorer ---------------------------------------------------------

    if (XMLHttp == null)
    {
    XMLHttp = new XMLHttpRequest();
    }
    return XMLHttp;
    }

    // Make the XMLHttpRequest object
    var http = GetXmlHttpObject();

    function sendRequest(page) {

    // Open PHP script for requests
    http.open('get', page);
    http.onreadystatechange = handleResponse;
    http.send(null);

    }

    function handleResponse() {

    if(http.readyState == 4 && http.status == 200){

    // Text returned FROM the PHP script
    var response = http.responseText;

    if(response) {
    // UPDATE ajaxTest content
    document.getElementById("msgstatus").innerHTML = response;
    }

    }

    }

    function repeatloop()
    {
    sendRequest('featured.php'); // replace "inbox-status.php" with your php page's url
    setTimeout("repeatloop()", 20000);
    }

    window.onload=function() {
    repeatloop();
    }
    </script>

    Again, this works perfectly in Firefox, but not IE7.

    Any further help would be great as this is driving me nuts!!

  6. #6
    this is really strange i've tried the above code in ie 7 and it works without a hitch...

    oooh sorrie did change this:

    if(http.readyState == 4 && http.status == 200){

    to:
    if (this.xmlHtml.readyState == 4 || this.xmlHtml.readyState == "complete")

    found this info:
    status was implemented in MSXML 2.0 and later
    ( shouldn't be a problem for ie 7 but otherwise i didn't get the response... )
    Last edited by borrob; January 31st, 2008 at 03:39 AM.

  7. #7
    Hi borrob,

    Sorry to be a pain, but you obviously know more about ajax than I do.

    I've changed the line you suggested above, but I now get the error: 'this.xmlHtml.readyState' is null or not an object.

    Was there anywhere else I was supposed to edit the code?

  8. #8

    ok my mistake

    Sorrie about that i'm pasting this from my own ajax class and that has a this. Just remove the this. from the statements.
    But to save you some time, and because i think it's a better way of doing this i will include my ajax class here that you are free to use there's an example in there to.....

    you have to unziip it first to a place that is coontrolled by a php server and open the ajax_example.php file
    Attached Files Attached Files

  9. #9

    Don't work

    I'm sorry but it doesn't work with IE7...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012