What is Ajax
AJAX is the acronym for Asynchronous JAvascript and Xml, is not a language rather it is a technique to create interactive or dynamic web page.
AJAX provides XMLHttpRequest objects to interact with the server. AJAX requests small pieces of information, not the whole page using HTTP requests.
AJAX works well with different languages, and it does not require any special server it uses the web page instead.
To know AJAX in detail please visit our website: http://www.roseindia.net/programming-tutorial/Ajax
Following tutorial will help you to learn PHP and AJAX.
In the following example you can see that whenever you enter any alphabet, some suggestions related with that letter will be displayed below the textbox, without refreshing the page, and this is the magic of AJAX. It receives data from the sever in small pieces instead of the whole page.
The example is divided in three parts:
phpAjax.html
html><
<
head>script type="text/javascript" src="phpAjax.js"></script><
</
head><
body>b><u>Enter Names Below</u></b><
<
p/><
form>First Name:
<input type="text" id="txt1" onkeyup="showHint(this.value)" /><
p>Probable Names are:</
form><
span id="probable"></span></
body></
html>phpAjax.php
<?php
$a
[]="Avishek";$a
[]="Bijay";$a
[]="Chunnilal";$a
[]="Deepak";$a
[]="Dipesh";$a
[]="Elesh";$a
[]="Farha";$s
=$_GET["s"];if
(strlen($s) > 0){
$hint=""; for($i=0; $i<count($a); $i++){
if (strtolower($s)==strtolower(substr($a[$i],0,strlen($s)))){
if ($hint==""){
$hint=$a[$i];}
else { $hint=$hint." , ".$a[$i];}
}
}
}
if
($hint == ""){
$response="Currently Unavailable";}
else
{ $response=$hint;}
echo
$response;?>
phpAjax.js
xmlhttpvar
function
showHint(str){
if
(str.length==0){
"probable").innerHTML=""; return;document.getElementById(
}
xmlhttp=GetXmlHttpObject();
if
(xmlhttp==null){
alert (
"Sorry, your browser is compatible!!!"); return;}
url="phpAjax.php";var
url=url+
"?s="+str;url=url+
"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open(
"GET",url,true);xmlhttp.send(
null);}
function stateChanged()
{
{
document.getElementById("probable").innerHTML=xmlhttp.responseText;
}
}
function GetXmlHttpObject()
{
if
(window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safarireturn new XMLHttpRequest();
}
if
(window.ActiveXObject){
// code for IE6, IE5return new ActiveXObject("Microsoft.XMLHTTP");
}
return
null;}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: PHP Ajax View All Comments
Post your Comment