Well I'm just starting out with
ajax, and tryed making the simplest thing to start off with. When you click the button, it shows the date and how many seconds has gone. This is what I've got so far.
HTML Page (
ajax.
html):
- Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Testing AJAX</title>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<input type="button" name="ajax" id="name" onkeyup="getTime()" value="Check Time">
<div id="outputMessage"></div>
</body>
</html>
JavaScript Page (
ajax.
js):
- Code: Select all
function createRequest () {
try {
request = new XMLHttpRequest ();
} catch (trymicrosoft) {
try {
request = new ActiveXObjext("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
if (request == null)
alert ("Error creating the request object!");
}
function getTime() {
createRequest();
var url = "http://www.jack-day.com/ajax.php";
request.open("GET", url, true);
onreadystatechange = processTime();
request.send(null);
}
function processTime(){
if (request.readyState==4 || request.readyState=="complete")
{
document.getElementById("outputMessage").innerHTML = request.responseText;
}
}
PHP Page (
ajax.
php):
- PHP: Select all
<?php
$time = date("d/m/y - s");
echo $time;
?>
Thanks in advance