Ajax And PHP - PHP Lecture 12
Ajax And PHP - PHP Lecture 12
http://freepdf-books.com
What is Ajax?
Asynchronous JavaScript And XML
Jesse James Garett coined term AJAX in his article:
“AJAX : A new approach h for
f a new application”
li i ”
Term used in 2005, technologies existed earlier
Ajax is just a new style of designing application
Manipulation of GUI elements
Saving state in the background
Drag portal sub‐window, position saved automatically
Loading new state in the background
Refresh a feed every 10min
Switch to a different portal tab
Basically app in the browser
See http://docs.google.com
Web Tech 1. AJAX 2
http://freepdf-books.com
Ajax is based on
HTML (or XHTML) and CSS
Presenting
g information
DOM ‐ Document Object Model
Dynamic display and interaction with the information
XML, XSLT (Optional)
Data interchange and manipulation
XMLHttpRequest
Asynchronous data retrieval object
http
p://adaptivepath
h.com/publications/essays/arch
hives/000385.ph
hp
Classical Web Apps vs. Ajax (1)
http://freepdf-books.com
Web Tech 1. AJAX
XMLHttpRequest (1)
Keystone of Ajax is XMLHttpRequest object.
XMLHttpRequest
p q object
j in JavaScript
p
Client‐side
component
Makes HTTP requests (both GET and POST) without
bl ki the
blocking h user
Introduced in MS Internet Explorer 5
Being standardized by W3C
Now widely supported in most of the browsers
Mozilla Firefox, Safari 1.2, Opera 7.6
Example
p
var req = new XMLHttpRequest(); // Non‐IE
var req
q = new ActiveXObject("Microsoft.XMLHTTP");
j ( );
Example
req.open("POST",
q p ( , "phone.php")
p p p)
req.send("name=Tanveer&city=Rawalpindi");
Example
req.onreadystatechange = stateHandler;
Example:
if (req.readyState == 4) {
if (req
(req.status
status == 200) { //200 means OK (no error)
success();
}
}
Web Tech 1. AJAX 12
http://freepdf-books.com
Ajax
j Example
p ‐1
<body>
<form>
Select a User:
<select name="users" onChange="showUser(this.value)">
<option value="0">Select Contact Group</option>
<option value="1">Friends</option>
<option value="2">Family</option>
<option value="3">Classfellows</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
return null;
}
Web Tech 1. AJAX 14
http://freepdf-books.com
Ajax
j Example
p ‐3
<?php
$q=$_GET["q"]; while($row = mysql_fetch_array($result))
$con = mysql_connect('localhost', 'root', ''); {
mysql_select_db("addressbook", $con); echo "<tr>";
$sql="SELECT
$sql "SELECT * FROM contacts WHERE groupid
gro pid = ' echo "<td>" . $ro
$row['firstname']
['firstname'] . "</td>";
"</td>"
".$q."‘ ";
echo "<td>" . $row['lastname'] . "</td>";
$result = mysql_query($sql); echo "<td>" . $row['city'] . "</td>";
echo "<td>"
<td> . $row['email']
$row[ email ] . "</td>";
</td> ;
echo "<table border='1'>
// echo "<td>" . $row['Job'] . "</td>";
<tr> echo "</tr>";
<th>Firstname</th>
}
<th>Lastname</th>
echo "</table>";
<th>City</th>
<th>E‐mail</th>
mysql_close($con);
</tr>"
</tr>";
?>
Many other…
other