This will check input names against those available in a database. It will check from a string entered to find all that have that substring in them, and provide an updated suggestion to the user.
We have entered some usernames into our server side code. Using bob or mike will illistrate our code in action.
Now we need to define a function to run [ajaxNow()] in response to the button press.
<script>
function ajaxNow()
{
var _value=document.testform.one.value;
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;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.testform.two.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","/ajax/test2_ajax.php?value="+_value,true);
xmlHttp.send(null);
}
</scriptgt;
Note in the open() call we pass the variable _value_ to the ajax php file using the GET or URL-Rewriting method.
Here is the PHP code - so you can see it (we just pretend to have a DB..)
Note that all PHP variables above have a space inserted after the dollar sign, and in real PHP code this space would be removed.
You can click here to run the code