Postanowiłem troszkę poszerzyć wiedzę na temat technologii AJAX, jednak mój kod nie jest sprawy, bardzo prosiłbym o sprawdzenie i kilka sugestii na ten temat
Oto pliki jakie posiadam
Index.html:
<html>
<head>
<title>AJAX 1.0</title>
<script type="text/javascript" src="quickstart.js"></script>
</head>
<body onload='process()'>
Server pragnie poznac twoje imie
<input type="text" id="myName">
<div id="divMessage">
</body>
</html>
quickstart.js:
// JavaScript Document
var xmlHttp = createXmlHttpReqestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject)
{
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
catch(e){
xmlHttp = false;
}
}
else
{
try{
xmlHttp = new XMLHttpRequest();
}
catch(e){
xmlHttp = false;
}
}
if(!xmlHttp)
alert("blad")
else
return xmlHttp;
}
function process(){
if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
name = encodeURIComponent(document.getElementById("myName").value);
xmlHttp.open("GET", "quickstart.php?name=" + name, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}
else
setTimeout('process()', 1000);
}
function handleServerResponse{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
helloMessage = xmlDocumentElement.firstChild.data;
document.getElementById("divMessage").innerHTML = '<i>' + helloMessage + '</i>';
setTimeout('process()', 1000);
}
else
{
alert("wystapil blad");
}
}
}
quickstart.php
<?php
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
$name = $_GET['name'];
$userNames = array('mateusz', 'yoda');
if(in_array(strtoupper($name), $userNames))
echo 'witaj mistrzu '.htmlentities($name).'!';
else if (trim($name) == '')
echo 'podaj swoje imie!';
else
echo 'Nie znam Cie!';
echo '</response>'
?>
Z góry dziękuje. Eldiane