Simple Jquery Ajax Auto Refreshing DIV

So you have a div that you would like to be up to date on your site without having the page refresh? This small script will allow you do this. Let's say that you want to show the total number of users on your site as your site is blowing up and you want visitors to see the number grow. firstly...load jquery
<script src=""></script>
next, we need to build the script in the "head" section of your page. This script targets a div with a loaded php file and sets how often the div should be reloaded
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug.  without it, IE will only load the first number and will never refresh
setInterval(function() {
}, 3000); // the "3000" here refers to the time to refresh the div.  it is in milliseconds. 
// ]]></script>
here are the contents of "userCount.php"
require "/path/to/your/database/connection/connection.php";
$userCount = mysql_query( "SELECT * FROM users" ) or die("SELECT Error: ".mysql_error());
$numRows = mysql_num_rows($userCount);
echo $numRows;
here is the div in the html. you can place "loading users..." or a "loading" image inside of the div and it will be replaced by the results of "userCount.php"
<div id="results">Loading users...</div>
Post a comment
  1. stanley

    thanks for this, its really nice and works perfectly well, but i am having a little problem, after loading the refresh script, every link in my page stopped working except those withing the div that is been refreshed. i don't know if you can give me any suggestion on how to go about it. thanks

    • Matthew Price

      Hello Do you have a reference URL? this script shouldn't affect other links on the page like this Matt

      • stanley

        please i don't mean to disturb you, just having a little problem with my project. i am building a web application that loads an external content in xml format, can you help me with php code that can get and manipulate the xml file.

        • Matthew Price

          No Problem! Do you have an example of how your XML file is formatted? If you don't want to post it here, feel free to email me and i can take a look I have a couple of different XML Parsers that i use, so one of them should do the trick Matt

          • stanley

            please i am sorry for getting back late, i am having serious issue with my dreamweaver div, please can anyone in this forum help?, i am building a site with div layout and everything have been working fine until few day back when i noticed that i cannot place a div on a div and whenever i insert a div, it shows at the code view but not visible in the design view and css panel. please any help would be greatly appriciated!

      • stanley

        thank you for your help, it's now working!

  2. derrickrobinsonDerrick

    Thank you so much! I've been going crazy with div refresh, that wasn't working correctly. I thought it looked like a browser cache issue, and after seeing your article and especially line 3: $.ajaxSetup({ cache: false }); my div refresh now works :-) Funnily enough though, I always use Chrome, so this doesn't seem to just affect IE (for once). Thanks, Derrick

  3. Irfan Masih

    This is working well ! visit: Thanks Irfan Masih

Real Time Web Analytics ^