Ajax PDF
Ajax PDF
Ajax PDF
Definition of AJAX
AJAX, which stands
for Asynchronous JavaScript And XML, is a
technique that allows web pages to be
updated asynchronously, which means
that the browser doesn't need to reload
the entire page when only a small bit of
data on the page has changed.
Ajax allows to create server connections
in the background while a user is
interacting with a Web front-end. These
connections can be created
asynchronously, which means that the
user need not wait until the server replies.
AJAX allows web pages to communicate
with the server behind the scenes. AJAX
passes only the updated information to
and from the server.
The word "asynchronous" means that the
user need not wait until the server
replies.
(Asynchronous (in Ajax) processes
incoming requests in a constant event
stack and sends small requests one
after the other without waiting for
responses. In other words, asynchronous
ajax call allow the next line of code to
execute, whereas synchronous call stop
JavaScript execution until the response
from server.)
Ajax is client side script that
communicates with web server.
Synchronous vs Asynchronous
Synchronous (Classic Web-Application Model)
A synchronous request blocks the client until
operation completes i.e. browser is unresponsive.
In such case, javascript engine of the browser is
blocked.
Asynchronous (AJAX Web-Application Model)
An asynchronous request doesn’t block the
client i.e. browser is responsive. At that time,
user can perform another operations also. In
such case, javascript engine of the browser is
not blocked.
Why synchronization is used
For example, you might need to handle
some transaction processing in which the
order is important. Consider a case in
which a web page needs to return a
confirmation page after the user
clicked something. This task requires
synchronizing the requests.
It is not a programming language.
It 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 is no programming or script language, no
new invention and no separate Web service,
module or plug-in. It is a group of inter-related
technologies like javascript, dom, xml, html, css
etc.
Following technology are used in AJAX:
HTML and CSS
These technologies are used for displaying
content and style. It is mainly used for
presentation.
DOM : It is used for dynamic display and
interaction with data.
XML or JSON
For carrying data to and from server. JSON
(Javascript Object Notation) is like XML but
short and faster than XML.
XMLHttpRequest
For asynchronous communication between
client and server.
Javascript
It is used to bring above technologies
together. It used for Client-side validation
and validate user input in an HTML form
before sending the data to a server.
What is the DOM?
The Document Object Model (DOM) is a
programming interface for HTML and
XML documents.
Your html
◦ Document :
page(document)
◦ Object: Elements and
attributes in your page
◦ Model: in DOM, tree structure of
html elements.
◦ It create tree structure of elements and
attributes of your html document in web
page.
Introduction to XML
XML stands for Extensible Markup
Language.
XML is a software and hardware
independent tool for storing and
transporting data.
XML is markup language much like HTML.
XML was designed to be self-descriptive.
XML is designed to carry data, not to
display data.
Difference Between XML
and HTML
XML was designed to carry data with
focus on what data is
HTML was designed to display data with
focus on how data looks.
XML tags are not predefined. You must
define your own tags.
Why use Ajax ?
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 can be used with javascript,
jquery and asp.net.(just way is different)
AJAX application might use XML to
send data, it could also use just plain
text or JSON text. But generally, it uses
an XMLHttpRequest object in your
browser (to request data from the
server) and JavaScript to display the
data.
l Google Suggest
AJAX was made popular in 2005 by Google,
with Google Suggest.
Google Suggest is using AJAX to create a very
dynamic web interface: When you start typing
in Google's search box, a JavaScript sends the
letters off to a server and the server returns
a list of suggestions without reload that page.
Features of AJAX
Uses of AJAX
There are too many web applications
running on the web that are using ajax
technology like;
Gmail
Facebook
Twitter
Google
Youtube
Ajax are mainly used where you want
to reload a part of web page but not
complete website or web-page. It make
any web-page faster.
XMLHttpRequest Object in Ajax
req.onReadyStateChange=function(){
if(req.readyState==4 && req.status==200){
document.getElementbyId('city').innerHTML=req.
responseText;
}
} }
</script> </body> </html>
//response.php
<?php
$data=$_GET['datavalue'];
$a=array('Indore','Ujjain');
$b=array('Bihar','Luckhnow');
if($data=="MP"){
foreach($a as $a1){
echo "<option> $a1 </option>";
}
}
?>
<div id="loaddata">
<h2>Change that</h2>
</div>
<button onclick="myfunc()">Check</button>
<script type="text/javascript">
function myfunc(){
var req = new XMLHttpRequest();
req.open('GET','data.html',true);
req.send();
req.onreadystatechange = function(){
if(req.readyState == 4 && req.status ==
200){
document.
getElementById('loaddata').innerHTML=req.
responseText;
} }
} </script>
Ex 2
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").
innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status ==
200) {
document.getElementById("txtHint").
innerHTML = this.responseText;
}
}
xmlhttp.open("GET", "gethint.php?q="+str,
true);
xmlhttp.send();
}
}
</script> </head>
<body>
if(!$con) {
die('Not Connected To Server');
}
//Connection to database
if(!mysqli_select_db($con, 'test')) {
echo 'Database Not Selected';
}
//Create variables
$name = $_POST['name'];
$age = $_POST['age'];
$query = mysqli_query($con,"SELECT *
FROM user WHERE name='$name' OR
age='$age'");
$sql = "INSERT INTO user (name, age)
VALUES ('$name', '$age')";