bebo india logo
| More

AJAX Example Detailed Explanation
The Complete JavaScript and HTML File

Code for the complete html file is given below:-

<html>
<head>
<script type="text/JavaScript"><script type="text/javascript" language="JavaScript1.4">
var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHints").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="gethint.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHints").innerHTML=xmlHttp.responseText
}
}

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}
</script>
</head>

<body>

<form>
First Name:
<input type="text" id="txtSample"
onkeyup="showHint(this.value)">
</form>

<p>Suggestions: <span id="txtHints"></span></p>

</body>
</html>



getHint.php File

gethint.asp file is called each time a user enters a character into the text box. Here is the full gethint.php file.

<?php
// Fill up array with names
$a[]="Anna";
$a[]="Binu";
$a[]="Cicily";
$a[]="Diana";
$a[]="Eva";
$a[]="Flonor";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kirty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Shimna";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Dolly";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunitha";
$a[]="Thampy";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vinu";

//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}

// Set output to "no suggestion" if no hint where found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}

//output the response
echo $response;
?>



Make one html file and one php file and try your luck!

Next -> AJAX ASP Scripting .
 


Find A Job
Invisibility
Asian Games
Salman Khan
 


 


 

all rights reserved. contact @ webmaster