Let’s say you want to bring down a list of names from the server and display them without reloading the page.
I’ll show you a simple way to do it using the Ajax technology.
1. Create a form:
<form name="frm" action="#" autocomplete=off onSubmit="getIt();return false;">
<fieldset>
<label for "id">Enter name:</label>
<input size="20" type="text" id=’id’ name="name" value='’><BR>
<input type="submit" name="doit" value="submit">
</fieldset>
</form>
The submit even will call a JavaScript function getIt(). We’ll get to it later.
2. Create a div
<div id=result> </div>
3. Get a reference to XMLHttpRequest object.
function getHTTPObject() {
if (typeof XMLHttpRequest != ‘undefined’)
{ return new XMLHttpRequest(); }
try { return new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e)
{ try { return new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {} }
return false; }
var xmlhttp = getHTTPObject();
4. Create a getIt() function:
function getIt( ) {
if( xmlhttp) {
var url =’servlet/myClass’;
var target = document.getElementById("id");
url += "?id=" + encodeURIComponent(target.value);
xmlhttp.open(’GET’, url, true);
xmlhttp.send(null);
var divResult =document.getElementById(’result’);
xmlhttp.onreadystatechange = function (){
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
divResult.innerHTML = xmlhttp.responseText;
}
}
}
}
}