UNIT III - AJAX With PHP
UNIT III - AJAX With PHP
UNIT III - AJAX With PHP
Introduction
• AJAX - Asynchronous JavaScript And XML
• AJAX is not a programming language
• AJAX is a technique for accessing web servers
from a web page
• AJAX just uses a combination of:
– A browser built-in XMLHttpRequest object (to
request data from a web server)
– JavaScript and HTML DOM (to display or use the
data)
• AJAX was popularized in 2005 by Google (with
Google suggest)
AJAX cannot work independently. It is used in combination
with other technologies to create interactive web pages.
• JavaScript:
– Loosely typed scripting language.
– JavaScript function is called when an event occurs in a page.
– Glue for the whole AJAX operation.
• DOM:
– API for accessing and manipulating structured documents.
– Represents the structure of XML and HTML documents.
• CSS:
– Allows for a clear separation of the presentation style from
the content and may be changed programmatically by
JavaScript
• XMLHttpRequest:
– JavaScript object that performs asynchronous interaction
with the server.
Why AJAX ??
• AJAX allows web pages to be updated
asynchronously by exchanging data with a
web server behind the scenes. This means
that it is possible to update parts of a web
page, without reloading the whole page
Famous web applications that make use of AJAX
• Google Maps:
– A user can drag an entire map by using the mouse, rather
than clicking on a button.
– http://maps.google.com/
• Google Suggest:
– As you type, Google will offer suggestions. Use the arrow
keys to navigate the results.
– http://www.google.com/webhp?complete=1&hl=en
• Gmail:
– Gmail is a webmail, built on the idea that email can be more
intuitive, efficient and useful.
– http://gmail.com/
• Yahoo Maps:
– Now it's even easier and more fun to get where you're going!
– http://maps.yahoo.com/
Pros and Cons of AJAX
• Pros:
– Allows web applications to interact with data on
the server
– Avoid clunky GET/POST send/receive interfaces
– web apps look more and more like real
applications
– Some applications can only be realized this way
– Eg: Google Suggest offers interactive access to one of the
largest data collections in the world
– For office style applications, user's data is stored
on a reliable server, accessable from any web
browser
• Cons:
– Tough to make compatible across all browsers
– Should have a low-latency connection to the
server
– Can be server intensive
– Eg: Google Suggest generates a search for
every keystroke entered
Working of AJAX
1. An event occurs in a web page (the page is
loaded, a button is clicked)
2. An XMLHttpRequest object is created by
JavaScript
3. The XMLHttpRequest object sends a request to a
web server
4. The server processes the request
5. The server sends a response back to the web
page
6. The response is read by JavaScript
7. Proper action (like page update) is performed by
JavaScript
XMLHttpRequest Object
• The XMLHttpRequest object can be used to
exchange data with a web server behind the
scenes. This means that it is possible to update
parts of a web page, without reloading the
whole page.
• Almost All modern browsers support
the XMLHttpRequest object.
Create an XMLHttpRequest Object
variable = new ActiveXObject("Microsoft.XMLHTTP");
XMLHttpRequest Object Methods
XMLHttpRequest Object Properties
Sending Request To a Server
• The XMLHttpRequest object is used to
exchange data with a server.
• To send a request to a server, we use the
open() and send() methods of
the XMLHttpRequest object
• Syntax:
– xhttp.open("GET", “URL", true);
– xhttp.send();
• To POST data like an HTML form, add an HTTP header
with setRequestHeader()
– xhttp.setRequestHeader("Content-
type", "application/x-www-form-urlencoded");
• URL: is an address to a file on a server
• Asynchronous - True or False?
– Server requests should be sent asynchronously
– By sending asynchronously, the JavaScript does not
have to wait for the server response, but can
instead:
– Execute other scripts while waiting for server
response
– Deal with the response after the response is ready
onreadystatechange Property