what i'm trying to do is find all results that come after the page has loaded...

the <div id="last">32</div> is the latest id that is displayed on the page, this i will have done with php later, but what i want to do, is when retrieving the data back from test.php i want this number to be replaced with $lastmessage which comes from test.php

my question is: how do i retrieve the data as variables, so that i can replace the innerHTML of div id=last with only the variable $lastmessage, and add $messages to the div id=messages

my code:

index.php

HTML Code:
<script type="text/javascript">
	function refreshMessages(lastID) {
		if (lastID.length==0) {
			document.getElementById("messages").innerHTML="";
		return;  
		}
		if (window.XMLHttpRequest) {
			xmlHttp=new XMLHttpRequest();  
		} else {  
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  
		}
		var url="test.php?q=" + lastID;
		xmlHttp.open("GET",url,false);
		xmlHttp.send();
		e = document.getElementById("messages");
		e.innerHTML=e.innerHTML+xmlHttp.responseText;
	}
</script>
<div id="last">32</div>
<input type="button" onclick="refreshMessages(document.getElementById('last').innerHTML);" value="Click"/>
<div id="messages"></div>
test.php

PHP Code:
<?php
include("config.php"); // config.php only connects to the database

$query_string mysql_real_escape_string($_GET['q']);

$res mysql_query("SELECT * FROM chat WHERE id>'" $query_string "'") or die(mysql_error());
$rowCount mysql_num_rows($res);

if (
$rowCount 1) {
} else {
$messages ""// define the variable
    
while($row mysql_fetch_array($res)) {
    
$uid $row['userid'];
        
$res2 mysql_query("SELECT * FROM login WHERE uid='" $uid "'") or die(mysql_error());
        while(
$row2 mysql_fetch_array($res2)) {
        
$username $row2['display'];
        }
    
$time $row['date'];
    
$message $row['message'];
    
$messages .= '<p style="padding:0px;margin:0px;margin-top:3px;width:380px;text-indent:5px;">' $username ' - [<font style="font-size:11px;">' date("H:i - d/m"$row['date']) . '</font>]<br /><font style="margin-left:10px;">' $row['message'] . '</font></p>';
    
$lastmessage $row['id'];
    }
    echo 
$messages;
}
?>