Environmental Site Logo
 
Ajax Home
Ajax Example ASP
Ajax Example PHP
What is AJAX?
 
 

Sample AJAX Example


See Previous Page for the complete example

The Complete JavaScript and HTML File for PHP Scripting

Code for the complete html file for PHP 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.php 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 asp file and try your luck!


More on this site
Agricultural Education
Environmental News
Funny SMS
Computers & Technical
Agri Search Engine
Funny Videos
Bollywood Celebrities
Funny Games
Help Children!
ASP Bulk Email
Secure a Job New!
Mother Teresa
Sex Education
 
Aishwarya Rai Movie Clip



© All rights reserved
Send your comments and suggestions to Webmaster. Best viewed in 800x600 with medium text size in Firefox 2