Ajax Page With PHP
The AJAX HTML Page
This is the HTML page. It contains a simple HTML form and a link to a JavaScript.
First Name:
<input type=”text” id=”txt1″
onkeyup=”showHint(this.value)”>
Suggestions:
The JavaScript code is listed below.
The AJAX JavaScript
This is the JavaScript code, stored in the file “clienthint.js”:
var xmlHttp
function showHint(str)
{
if (str.length==0)
{
document.getElementById(”txtHint”).innerHTML=”";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert (”Your browser does not support AJAX!”);
return;
}
var url=”tutorialz.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)
{
document.getElementById(”txtHint”).innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(”Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}
The AJAX Server Page -PHP
There is no such thing as an AJAX server. AJAX pages can be served by any internet server.
AJAX PHP Example
The code in the “tutorialz.php” page just checks an array of names and returns the corresponding names to the client:
AJAX PHP Example
The code above rewritten in PHP.
Note: Code below is tutorialz.php
PHP Example
< ?php
// Fill up array with names
$a[]="Google";
$a[]="Yahoo";
$a[]="Hotmail";
$a[]="Java";
$a[]="PHP";
$a[]="Asp";
$a[]="Ajax";
$a[]="Javascript";
$a[]="Ruby";
$a[]="Ecllipse";
$a[]="Dreamweaver";
$a[]="Flash";
$a[]="Photoshop";
$a[]="Fireworks";
$a[]="Flex";
$a[]="Balu";
$a[]="Shrijesh";
$a[]="Abilash";
$a[]="Vishnu";
$a[]="Shaneem";
$a[]="Azhar";
$a[]="Sarin";
$a[]="Fazi";
$a[]="Anoop";
$a[]="Sreeraj";
$a[]="Sreejith";
$a[]="Vipin";
$a[]="Binoj";
$a[]="Nithin";
$a[]="Rakku";
//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
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 were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
Related posts:
- Simple Ajax with PHP click tracker
- Get textbox value from dropdown using ajax and php
- XML Parsing Made Easy
- Automatic Ajax Loading Images With Prototype
- Auto refresh page with timer using javascript
- Caching of web page using php
- To Remove An Empty Element From An Array Using PHP

Very good site! I like it! Thanks! . [URL=http://my-test-doorway.myhost.com].[/URL]
because dirt cheap air fare tickets stuff I need should one way airline tickets .cheapest airfares to london from cle last minute discount flights also is the same as or recreational .In cheap tickets from seattle In Here outside flights bos to pit not on commercial airlines .aloha airlines lawsuit airlines from minneapolis to orlando Well student discount airline tickets sometime airline discount ticket military Cool stuff - cheap international flight
nice website keep going….
hmm wooww
yup..
good one..
A good website with good inputs….hats off
a coool……..site
HMM
best of its kind
a nice one..
fine
not bad..
http://community.fitnessanywhere.com/members/vbest12.aspx - christina milian nude christina milian nude
a nice one
Good ajax stuff and php
cool site
maudg3 http://google.com
That’s great tutorial we are able to receive the credit loans moreover, it opens up new possibilities.