null
null
Ajax
• AJAX stands for Asynchronous JavaScript and XML.
• AJAX allows you to send and receive data asynchronously without reloading the web page.
So it is fast.
• AJAX allows you to send only important information to the server not the entire page. So only
valuable data from the client side is routed to the server side. It makes your application
interactive and faster.
• Ajax is a technique for creating fast and dynamic web pages.
• It is a group of inter-related technologies like JavaScript, DOM, XML, HTML/XHTML, CSS,
XMLHttpRequest etc.
XMLHttpRequest
Property Description
After the browser has established a communication with the server, but
before the server has completed the response.
status Returns the status as a number (e.g., 404 for "Not Found" and 200 for
"OK").
Method Description
void open(method, URL, async, same as above but specifies username and
username, password) password.
objectvar=new ActiveXObject("Microsoft.XMLHTTP");
2. For the other web browsers, XMLHttpRequest is native object and it can be initiated
as follows.
<script>
var request;
function demo()
{
if(window.XMLHttpRequest)
{ //code for IE7+Chrome,opera
request=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{ //code for IE6. IE6
request=new ActiveXObject("Microsoft.XMLHTTP");
}
}
</script>
Example: A ajax program to read a text file and print the contents of the file when the user click
on the button.
<html><body>
<div id="demo">
<h2>Let AJAX change this text</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
<script>
function loadDoc() //info.txt file
{ <h1>AJAX</h1>
var xhttp; <p>AJAX is not a programming language.</p>
if(window.XMLHttpRequest) <p>AJAX is a technique for accessing web
{
servers from a web page.</p>
xhttp =new XMLHttpRequest();
} <p>AJAX stands for Asynchronous JavaScript
else if(window.ActiveXObject) And XML.</p>
{
xhttp =new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "info.txt", true);
xhttp.send();
}
</script>
</body></html>
Ajax PHP Framework
• There are various PHP frameworks available, like Ajax Core, cakePHP, Xajax, Sajax, Zephyr to
integrate Ajax with PHP. These frameworks supports Model, View and Controller (MVC)
architecture. They reduces the writing of same code and common function repeatedly.
• The Sajax is an Ajax based framework which generate Ajax-enabled JavaScript from many server-
side languages like PHP, ASP, ColdFusion, Perl, Python and ruby.
• This Sajax bridge execute server-side code and uses Object Remoting technique.
Example: A Ajax program to accept two numbers.
// ajax_add.html file
<html><body>
<script type="text/javascript">
function showadd(n1,n2)
{
var XHRobj;
if(window.XMLHttpRequest)
{
XHRobj= new XMLHttpRequest();
}
else
{
XHRobj=new ActiveXObject("Microsoft.XMLHTTP");
}
XHRobj.onreadystatechange=function()
{
if(XHRobj.readyState==4 && XHRobj.status==200)
{
document.getElementById("myDiv").innerHTML=XHRobj.responseText;
}
}
XHRobj.open("GET","ajaxadd.php?v1="+n1+"&v2="+n2,true);
XHRobj.send();
}
</script><form>
Enter no1 <input type=text name=n1 ><br>
Enter no2 <input type=text name=n2 ><br>
<input type =button value=Addition onclick="showadd(form.n1.value,form.n2.value)">
</form>
<div id="myDiv"></div>
</body></html>
// ajaxadd.php file
<?php
$n1=$_GET['v1'];
$n2=$_GET['v2'];
$c=$n1+$n2;
echo"Addition of two numbers is:$c";
?>
}
else echo $m;
}
?>
Handling XML Data Using PHP and AJAX
Example: Create an AJAX program to fetch information from an XML File.
==========================================================================
//BOOK.html
<html><head>
<script type="text/javascript">
function showTECH(str)
{
if(str=="")
{
document.getElementById("myDiv").innerHTML="";
return;
}
if(window.XMLHttpRequest)
{
XHRobj= new XMLHttpRequest();
}
else
{
XHRobj=new ActiveXObject("Microsoft.XMLHTTP");
}
XHRobj.onreadystatechange=function()
{
if(XHRobj.readyState==4 && XHRobj.status==200)
{
document.getElementById("myDiv").innerHTML=XHRobj.responseText;
}
}
XHRobj.open("GET","book.php?q="+str,true);
XHRobj.send();
}
</script></head><body><form>
select a TECH:
<select name="TECH" onchange="showTECH(this.value)">
<option value="">Select a TECH:</option>
<option value="PHP">PHP</option>
<option value="JAVA">JAVA</option>
<option value="TCS">TCS</option>
<option value="N/W">N/W</option>
<option value="ASP">ASP</option>
</select></form>
<div id="myDiv"><b>Book info will be listed here..</b></div>
</body></html>
//book.XML FILE
<?xml version="1.0"?>
<BOOKINFO>
<BOOK ID="L001">
<BNAME>PHP</BNAME>
<AUTHOR>NIRALIr</AUTHOR>
<YEAR>2015 </YEAR>
<PRICE>300</PRICE>
</BOOK>
<BOOK ID="L002">
<BNAME>N/W</BNAME>
<AUTHOR>NIRALIr</AUTHOR>
<YEAR>2015 </YEAR>
<PRICE>300</PRICE>
</BOOK>
<BOOK ID="L003">
<BNAME>JAVA</BNAME>
<AUTHOR>TECH-MAX</AUTHOR>
<YEAR>2015 </YEAR>
<PRICE>350</PRICE>
</BOOK>
<BOOK ID="L004">
<BNAME>TCS</BNAME>
<AUTHOR>VISION</AUTHOR>
<YEAR>2014 </YEAR>
<PRICE>400</PRICE>
</BOOK>
</BOOKINFO>
<html>
<head>
<script type="text/javascript">
function showdet(str)
{
if(str=="")
{
document.getElementById("myDiv").innerHTML="";
return;
}
if(window.XMLHttpRequest)
{
XHRobj= new XMLHttpRequest();
}
else
{
XHRobj=new ActiveXObject("Microsoft.XMLHTTP");
}
XHRobj.onreadystatechange=function()
{
if(XHRobj.readyState==4 && XHRobj.status==200)
{
document.getElementById("myDiv").innerHTML=XHRobj.responseText;
}
}
XHRobj.open("GET","actor.php?q="+str,true);
XHRobj.send();
}
</script></head><body><form>
enter Actor Name<input type =text name=n1>:
<br><input type =button value=getinfo onclick="showdet(form.n1.value)">
</form>
<div id="myDiv"><b>Movie Details will be listed here..</b></div>
</body>
</html>
// actor.php file
<?php
$q=$_GET["q"];
echo"Actor Name:$q<br>";
$c=pg_connect("host=localhost port=5432 dbname=demo user=postgres password=ssracs");
$sql="select movie.mno,mname,myear from movie,actor where movie.mno=actor.mno and
aname='".$q."'";
$result=pg_query($c,$sql);
echo"<table border='1'><tr><th>mno</th><th>mname</th><th>year</th></tr>";
while($r=pg_fetch_row($result))
{
echo "<tr>";
foreach( $r as $v)
{
echo "<td>$v</td>";
}
echo "</tr>";
}
echo"</table>";
?>