Take interest that we did not put a SUBMIT on this form.
Also note that we added an onkeypress event to run a function. So each time a key is pressed inside the focus of the first form-text box we will run our function.
Now we will add in that function. It will first attempt to determine which browser a person has, so we know which version of the AJAX HTTP header object to use:
<script>
function ajaxRun()
{
var xmlHttp;
try
{
/* Firefox, Opera 8.0+, Safari */
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
/* newer IE */
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
/* older IE */
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser is old and does not have AJAX support!");
return false;
}
}
}
}
</script>
Now we need to detect when the HTTP header object used in AJAX changes. There are 5 states:
Now the only thing left is to write some server code (in test_ajax.php).
Since it only needs the data for field two, we will return a random number.
NOTE: This is PHP code which goes in a separate file from the HTML/JAVASCRIPT/AJAX file we were previously describing.