Its pretty easy. Just mix AJAX with some PHP or other server language. EX.
script.js Code:
function init() {
httpObject = getHTTPObject();
if (httpObject != null) {
httpObject.open("GET", "process.php", true);
httpObject.send(null);
httpObject.onreadystatechange = display;
}
}
function display() {
if(httpObject.readyState == 4){
document.getElementById('content').innerHTML = httpObject.responseText;
}
}
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}
var httpObject = null; index.html Code:
<html>
<head>
<title>AJAX with PHP</title>
<script src="script.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
<a href="javascript: init();">Link</a>
<div id="content"></div>
process.php Code:
<?
echo('This info was pulled dynamically from AJAX');
?> The html link calls the init() function in the js file. The init function then calls the process.php file. You can pass query strings to this file for specifics. In the PHP file, you can do all your database specifics. Just echo the desired information and the display() function will get that info from the php file and display it in the specified div.