Introduction of AJAX in PHP

We use AJAX in PHP. Asynchronous JavaScript and XML (AJAX) is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the entire page. AJAX is based on internet standards, and uses a combination of:

Examle

//start here HTML code//

<html>

<head>

<script>

function showHint(str) {

if (str.length == 0) {

document.getElementById("txtHint").innerHTML = "";

return;

}

if (window.XMLHttpRequest) {

xmlhttp = new XMLHttpRequest();

}

else {

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.onreadystatechange = function () {

if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

document.getElementById("txtHint").innerHTML = xmlhttp.responseText;

}

}

xmlhttp.open("GET", "hint.php?q=" + str, true);

xmlhttp.send();

}

</script>

</head>

<body>

<div align="center">

<p><b><center>Start typing a name in the input field below:</center></b></p>

<form>

First name: <input type="text" onkeyup="showHint(this.value)">

</form>

<p>Suggestions: <span id="txtHint"></span></p>

</div>

</body>

</html

Note: If the input field is not empty, the showHint() function executes the following:

//start here PHP code//

<?php

// Fill up array with names

$a[]="Anna";$a[]="Big boss";$a[]="Chandan";$a[]="Dogy";$a[]="Elegabeth";$a[]="Feena";$a[]="Grand father";$a[]="Heena";$a[]="India";$a[]="Jony";$a[]="King khan";$a[]="Linda";

$a[]="Nina";$a[]="Ompuri";$a[]="Petunia";$a[]="Ameesha";$a[]="Raquel";$a[]="Cindy";$a[]="Doris";$a[]="Eve";$a[]="Evita";$a[]="Sunniva";$a[]="Tove";$a[]="Unni";$a[]="Violet";

$a[]="Liza";$a[]="sharad";$a[]="vinod kumar";$a[]="manish sir";$a[]="Vicky";

//get the q parameter from URL

$q=$_GET["q"];

//lookup all hints from array if length of q>0

if (strlen($q) > 0)

{

$hint="";

for($i=0; $i<count($a); $i++)

{

if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))

{

if ($hint=="")

{

$hint=$a[$i];

}

else

{

$hint=$hint." , ".$a[$i];

}

}

}

}

// Set output to "no suggestion" if no hint were found

// or to the correct values

if ($hint == "")

{

$response="no suggestion";

}

elses

{

$response=$hint;

}

//output the response

echo $response;

?>

Explanation: If there is any text sent from the JavaScript (strlen($q) > 0), then the following happens:

Output:

ajax1 use in php.jpg


ajax2 use in php.jpg
ajax3 use in php.jpg