I am new to using AJAX and am trying to implement paging of db results without a page refresh. However, I don't know the best/easiest way to send and process the php results. If I send a text string back, the div gets updated, but if I try to create and send back a table, I'm not getting any results. Here is the php code: (I tried sending/ retreving the data using GET and then tried just passing the next start position and fetching the query string, but I don't seem to be able to get formatted results back using either method. Is one method (GET or just passing the number)faster/preferable to the other?.
<?php require_once('../connections/hcfi.php'); ?>
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
//--- get the query string
$$pageNum_LitSet=(int)($_SERVER['QUERY_STRING']);
$maxRows_LitSet = 25;
$startRow_LitSet = $pageNum_LitSet $maxRows_LitSet;
mysql_select_db($database_hcfi, $hcfi);
$query_LitSet = "SELECT FROM literature";
$query_limit_LitSet = sprintf("%s LIMIT %d, %d", $query_LitSet, $startRow_LitSet, $maxRows_LitSet);
$LitSet = mysql_query($query_limit_LitSet, $hcfi) or die(mysql_error());
$row_LitSet = mysql_fetch_assoc($LitSet);
$col[1]="CCCCCC";
$col[-1]="DDDDDD";
$c=1;
$txt="";
/
$txt="hi there";
echo "$txt";
exit;
above works and div gets updated
loop below doesn't work
/
do
{
$c *= -1;
$rowN++;
$txt .="<tr bgcolor=\"$col[$c]\" onMouseOver='this.bgColor=\"#CCFFCC\"' onMouseOut='this.bgColor=\"$col[$c]\"' >";
$txt .="<td align=\"right\">\"$\"$row_LitSet['Price'] \".00\" <br /><a href='cart.php?<?php echo($row_LitSet['litNum']) ?>' onclick='add(<?php echo($row_LitSet['litNum']) ?>);return false;'><img src='images/basket.jpg' width='34' height='36' onclick=\"addtocart($row_LitSet['litNum']) )\"></a></td>";
} while ($row_LitSet = mysql_fetch_assoc($LitSet));
echo "$txt ";
exit;
?>
And here is the js file:
current=0; //-- what page # is showing
function createRequestObject() //--- make an xmlhttp object
{
var xmlhttp;
/@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = createRequestObject();
function getInfo(itemCode) //--- called when user wants to page results
{
current += itemCode;
alert("item= "+itemCode+ " current= "+ current+"---");
http.open('get', 'data3.php?' + current);
http.onreadystatechange = handleInfo;
http.send(null);
}
function handleInfo()
{
if(http.readyState == 4)
{
if(http.status == 200)
{
var response = http.responseText;
alert("response="+response+"====")
document.getElementById('litDiv').innerHTML = response;
}
}
}
The html page calls getinfo passing it 1 or -1 to go forward or backward in the paging process. The var current holds the current page number as is updated with the value passed in.
If I try writing the code another way, I get the results back, but the table doesn't get displayed. I can alert the responsetext and the table appears to be there, but the div doesn't get updated. Here is a link to that page: http://www.hcfi.org/litTEST2.php
Thanks for any help.
brad