Dynamically refreshing a page using AJAX and pulling date from a mysql database |
|
|
I got it working by using a modified version of the following:
http://www.brightcherry.co.uk/ 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,$ $db = mysql_select_db($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... |
|
Powered by
KBPublisher (Knowledge base software)