Azazia Software Knowledgebase
Search:     Advanced search
Browse by category:
Contact Us

Dynamically refreshing a page using AJAX and pulling date from a mysql database

Add comment
Views: 401
Votes: 0
Comments: 0
Posted: 31 Aug, 2010
by: Patridge B.
Updated: 31 Aug, 2010
by: Patridge B.
I got it working by using a modified version of the following:

http://www.brightcherry.co.uk/scribbles/2009/02/26/jquery-auto-refresh-div-every-x-seconds/

Basically download the zip file here and use the jQuery.js file

http://www.brightcherry.co.uk/test/refresh.zip

In the response.php as outlined in the example, I placed all of my mysql_connect,mysql_query/etc. stuff, and then echoed out the results. Utilizing the provided JS (jquery-1.3.2.min.js)

##############################################
index.php
##############################################
<html>
<head>
<!-- For ease i'm just using a JQuery version hosted by JQuery- you can download any version and link to it locally -->
<BODY>
THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM...<BR><BR>
<script src="jquery-1.3.2.min.js"></script>

<script>
 $(document).ready(function() {
         $("#responsecontainer").load("response.php");
   var refreshId = setInterval(function() {
      $("#responsecontainer").load('response.php?randval='+ Math.random());
   }, 2000);
});
</script>
<div id="responsecontainer">
</div>
</body>

##############################################
response.php (as listed above) looks as follows:
##############################################

<html><body>
<?
$x=round($_GET['randval']);
$user="admin";
$dbhost="localhost";
$password="g0north";
$DATABASE="test";
$connection = mysql_pconnect($dbhost,$user,$password) or die ("couldn't connect to server:$user@$dbhost");
$db = mysql_select_db($DATABASE,$connection) or die ("couldn't connect to database: $user@$dbhost on $database");
$query="select id from toast_users order by id asc";
$result = mysql_query($query) or die ("couldn't execute query: $query .");
$rows = mysql_num_rows(result);
if (empty($x)) {$x=0;}
for ($i=$x;$i < $rows;$i++) {
echo "Number: $i<BR>";
}

?>

##############################################


*note, the random value was only left in to illustrate that the screen values actually refresh.

You can adjust the refresh interval in the index.php where the current value is 2000.

You also have to pass values between index.php and response.php using a HTTP GET (i.e. ?randval=)

Works great for me...
Attached files
file refresh.zip (20 kb)

Others in this Category
document PHP 5.3.0 Oracle OCI8 Instant Client 11.1.0.7.0 , Solaris 10: ERROR: ld.so.1: php: fatal: libnnz11.so: open failed: No such file or directory
document Compiling PHP 5.3.X and receive: ld.so.1: php: fatal: libsybdb.so.5: open failed: No such file or directory
document Recompile apache2 to support LDAP on Solaris 10



RSS