If you want to save yourself the time of having to learn all the intricacies of AJAX, using a JavaScript framework is a good shortcut (they're a great time saver in general). Using something like YUI, you can build AJAX functionality into your application with just a few lines of code.
Specifically, you'll want to use the YUI Connection Manager Component. YUI has excellent documentation, so it shouldn't be hard to figure it out yourself. If not, here's a short tutorial for beginners.
Inside your HTML front-end you should have something like this:
<!-- YUI source files -->
<script src="http://yui.yahooapis.com/2.7.0/build/yahoo/yahoo-min.js"></script>
<script src="http://yui.yahooapis.com/2.7.0/build/event/event-min.js"></script>
<script src="http://yui.yahooapis.com/2.7.0/build/connection/connection-min.js"></script>
<script>
var tiles = new Array();
function refreshTable() {
var sUrl = "ajax/table.php";
var responseSuccess = function(o) {
var root = o.responseXML.documentElement;
var rows = root.getElementsByTagName('row');
for (i=0; i<rows.length; i++) {
tiles[i] = new Array();
for (j=0; j<rows[i].childNodes.length; j++) {
tiles[i][j] = rows[i].childNodes[j].firstChild.nodeValue;
}
}
/*
Update your table using the tiles[][] 2D array.
/*
}
var responseFailure = function(o) {
// Failure handler
alert(o.statusText);
}
var callback = {
success:responseSuccess,
failure:responseFailure,
timeout:3000
}
var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
}
setInterval(refreshTable(),2000);
</script>
Inside your PHP back-end, you just need to generate XML data in the format of something like:
<table>
<row>
<tile>dirt</tile>
<tile>water</tile>
<tile>water</tile>
<tile>dirt</tile>
<tile>dirt</tile>
</row>
<row>
<tile>dirt</tile>
<tile>dirt</tile>
<tile>water</tile>
<tile>water</tile>
<tile>dirt</tile>
</row>
<row>
<tile>dirt</tile>
<tile>dirt</tile>
<tile>water</tile>
<tile>water</tile>
<tile>dirt</tile>
</row>
<row>
<tile>dirt</tile>
<tile>dirt</tile>
<tile>dirt</tile>
<tile>water</tile>
<tile>water</tile>
</row>
<row>
<tile>dirt</tile>
<tile>dirt</tile>
<tile>dirt</tile>
<tile>dirt</tile>
<tile>water</tile>
</row>
</table>
And that's the gist of it. If you need to pass arguments to the server-side PHP script, you just append them onto the URL using encodeURI() and access them using the $_GET[] superglobal.