Forum Moderators: coopster
Save this as ajax.js
function createXMLHttpRequest()
{
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
alert("XMLHttpRequest not supported");
return null;
}function ajaxFunction(postvars)
{
var xmlHttp;
xmlHttp = createXMLHttpRequest();
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById('ajaxresults').innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("POST","ajax.php",true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(postvars);
}
Save this as ajax.html
<html>
<head>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
var code = '123';
</script>
</head>
<body>
<a href="#" onclick="ajaxFunction('code='+code)">Click here to send var code to PHP with AJAX</a>
<hr />
<div style="background-color:#EEF" id="ajaxresults">PHP Returned data will go here</div>
</body>
</html>
Save this as ajax.php
<?php
print_r($_POST);
?>
In this example, ajax.html calls ajax.js to "talk to" ajax.php when you click on the link in ajax.html and populates the <div> with whatever is produced by ajax.php
You can use a POST or GET request to pass on variables to your ajax.php page, and do PHPish thing on that page and return the data to ajax.html
You can find a couple of good tutorials by googling "AJAX tutorial" or similar. I found the W3Schools one useful, and a few hours or trial and error.
function ajaxFunction(postvars)
{
var xmlHttp;
xmlHttp = createXMLHttpRequest();
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById('ajaxresults').innerHTML=xmlHttp.responseText;
*************************comparison*************************
}
}
xmlHttp.open("POST","ajax.php",true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(postvars);
}
Thank you brotherhood of LAN.. Time to explore more ajax and php.