AJAX with XMLHttpRequest Object

 

coding-zon-js-ajax-xmlhttprequest

Implementing AJAX with XMLHttpRequestObject and PHP MySQL

 

 

What is Ajax?

 

AJAX is a technique for accessing web servers from a web page. Updating the part of the webpage with server response without refreshing the page, is called AJAX technique. 

 

XMLHttpRequestObject predefined object in JavaScript.

 

All the modern browsers will support XMLHttpRequestObject.

 

AJAX stands for Asynchronous JavaScript And XML.

 

How it works?

 

The below picture depicts the Ajax technique flow between client and server.  

 

 

 
coding-zon-ajax-demo




  • First client request sent to server through XMLHttpRequestObject

  • The server processes the request and sends a response to the webpage.

  • Client no need to wait for response. Sending and receiving tasks are performed simultaneously.

 
 
 
 
 
  

SQL Query:  

 

CREATE TABLE IF NOT EXISTS `students` (
`id` int(11) NOT NULL AUTO_INCREMENT, 
`stuname` varchar(30) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6
 
 
Insert Data

INSERT INTO `students` (`id`, `stuname`,`age`) VALUES
(1, 'avani', 12),
(2, 'bhavya', 11),
(3, 'kavya', 12),
(4, 'divya', 10),
(5, 'gayatri', 14),
(6, 'avani', 12),
(7, 'bhavya', 11),
(8, 'kavya', 12),
(9, 'gayatri', 14),
(10, 'vivek', 14),
(11, 'veekshit', 14),
(12, 'vindya', 14);
 

Ajaxdb.html

 
<html>
<head>
<script type="text/javascript" src="AjaxDB.js"></script>
</head>
<body>
<form>
<div id="txtHint"><b>Person info will be listed here.</b></div>
<br />
Select a User:
<select name="students" onchange="showDetails(this.value)">
<option value="avani">avani</option>
<option value="bhavya">bhavya</option>
<option value="divya">divya</option>
<option value="gayatri">gayatri</option>
<option value="vivek">vivek</option>
<option value="veekshit">veekshit</option>
<option value="vindya">vindya</option> 
</select>
</form>
</body>
</html>



Ajaxdb.js 

 
function showDetails(str)
{

xmlhttp=new XMLHttpRequest();
if (xmlhttp==null)
{
alert ("This Browser does not support HTTP Request");
return;
}
var url="AjaxDB.php?q="+str;

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
 
 



Ajaxdb.php

 
<?php
 

$q=$_GET["q"];

$con = mysqli_connect('localhost', 'root', '', 'coding-zon');
 

if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
 

$sql="SELECT * FROM students WHERE stuname = '".$q."'";
//echo $sql;
$result = mysqli_query($con, $sql);
echo "<table border='1'><tr> <th>student id</th><th>student name</th><th>age</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";

echo "<td>" . $row['id'] . "</td>"; 

echo "<td>" . $row['stuname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";

echo "</tr>";
}
echo "</table>";
mysqli_close($con);
 

?>
 
 
 
 
 
 

Reference: The below are some of the ready state codes. In Ajaxdb.js we care about comparing if (xmlhttp.readyState==4). It means the server successfully executed the request and sent a response.

 
 
 /*

readyStates codes

0 = NUNSENT(A request that has not called opend() yet)
1 = OPENED (Opend but not sent)
2 = HEADERS_RECEIVED(Headers and status received)
3 =Loading(Downloading responseText)
4 =DONE (Data transmission complete)
*/

 
 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP