How to do auto timed refresh using JS that fetch content from sql db?

Discussion in 'PHP' started by ads2help, Oct 23, 2008.

  1. #1
    Lets say I have a php file where I can fetch content from sql using links like:

    then it will fetch all username's content. That not a problem..but now I want to do a timed auto refresh without refreshing the page. The content is displayed in a div. the php code is

    
    <div>
    <?php ..my fetch content scripts here.. ?>
    </div>
    
    PHP:
    I want to refresh the content inside the div only.
    I think I can do it using JS or Ajax right? but how?
    I did some web search but i cant find the solution. maybe i use wrong keyword or what?

    Thank you.
     
    ads2help, Oct 23, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    are you familiar with ajax?

    on the js side just put a t=setTimeout(ajaxfunction(),1000);
    and it will loop again based on the second parameter (time in milliseconds) and reload the same function..
    and thats it
     
    bartolay13, Oct 23, 2008 IP
    ads2help likes this.
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Nope i am not familiar with ajax. My normal JS is quite ok compared to Ajax. I can do the settimeout thing.

    But now I need the Ajax part which send query to the php file and update the content.

    Anyway, thank you. =D
    +rep
     
    ads2help, Oct 23, 2008 IP
  4. octalsystems

    octalsystems Well-Known Member

    Messages:
    352
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    135
    Digital Goods:
    1
    #4
    definitely you have to use the ajax as bartolay13 mentioned
    if u can not do ask for a professional ;)
     
    octalsystems, Oct 23, 2008 IP
  5. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #5
    in case you need professional help, hit me up, i can do it for you and teach you how i did it :)
     
    serialCoder, Oct 23, 2008 IP
  6. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #6
    i can post you some basic ajax code that will auto refresh the server side (i mean the page where your query goes in.)
     
    bartolay13, Oct 23, 2008 IP
  7. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #7
    @octalsystems : Yes I am looking for one
    @ bartolay13's : that would be nice thanks.
     
    ads2help, Oct 24, 2008 IP
  8. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #8
    any idea? thanks.
     
    ads2help, Oct 25, 2008 IP
  9. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #9
    Sillysoft, Oct 25, 2008 IP
  10. imphpguru

    imphpguru Active Member

    Messages:
    439
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #10
    
    function refreshVal(uname)
      {
    	
      var xmlHttp;
      try
        {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
        }
      catch (e)
        {
        // Internet Explorer
        try
          {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }
        catch (e)
          {
          try
            {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
          catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
          }
        }
        xmlHttp.onreadystatechange=function()
          {
          if(xmlHttp.readyState==4)
            {
    			document.getElementById('required_div').innerHTML  = xmlHttp.responseText;
            }
          }
        xmlHttp.open("GET","thefilewithphpcode.php?username="+uname,true);
        xmlHttp.send(null);
      }
    
    Code (markup):
    Now thefilewithphpcode is the file that has your phpcode. So every few seconds, which you can do by using

    setTimeout('refreshVal("'+document.getElementById('uname').value+'");',refreshInterval);

    Again this script may need some working so as to get the value of uname correctly but most of the cases, it should work.

    Thanks

    imphpguru
     
    imphpguru, Oct 25, 2008 IP
  11. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #11
    Thank you phpguru & Sillysoft =D
    Changed setTimeout to setInterval to suit my needs =D
     
    ads2help, Oct 26, 2008 IP