Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scrolling through ajaxs results

Status
Not open for further replies.

andyfresh

Technical User
Oct 4, 2005
33
GB
Hi all,

Im currently a beginner with ajax and trying to modify this simple ajax example. What I would like to be able to do is when the next link, which is located at the bottom of the html is click I would like the ajax to find the next record in the sql results and then for it to display this record. Can this be done in ajax??

PHP script:
Code:
<? 
require_once ('../../../templates/mysql_connect.php');
$page = $_GET["page"]; //This is the variable we retrieve through GET to know which row of content to retrieve

$sql = "SELECT commentsummary FROM comments where companyID = $page and rss = 1";

$query = mysql_query($sql) or die(mysql_error());

$r=mysql_fetch_assoc($query); //Set a mysql fetching variable for the query
  echo $r["commentsummary"]; //Echo out the content of the page we want
  
  ?>

Java script and html script
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<script language="javascript" type="text/javascript">
function getPage(page){
var xmlhttp=false; //Clear our fetching variable
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
        } catch (e) {
                try {
                        xmlhttp = new
                        ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
            } catch (E) {
                xmlhttp = false;
                        }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
        }
        var file = 'text1.php?page='; //This is the path to the file we just finished making *
    xmlhttp.open('GET', file + page, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ //Make sure there is something in the content variable
                      document.getElementById('content').innerHTML = content + "<br><br><font color=\"#CCCCCC\" size=\"1\" >PoweredBy </a></font>"; //Change the inner content of your div to the newly retrieved content ****
                }
        }
        }
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}
</script>
</head>
 
<body onload="getPage('157')">
<div id="content">
 
</div>
<div id="links" align="left">
<font size="1"><a href="javascript:getPage()">Next-></a></font>
</div>

</body>
</html>
 
this is not done with AJAX, this is done server side.

You know what the current record is right, so the link that fires the AJAX should also send the var RECID=x , the receiving sever side code will simply issue the SQL command to SELECT TOP 1 * FROM table WHERE RECID > value_of_RECID_on_URL.

hope this helps

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top