Home Php PHP Ajax
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

PHP Ajax

Advertisement
AJAX is the acronym for Asynchronous JAvascript and Xml, is not a language rather it is a technique to create interactive or dynamic web page.

PHP Ajax

     

What is Ajax

AJAX is the acronym for Asynchronous JAvascript and Xml, is not a language rather it is a technique to create interactive or dynamic web page.

AJAX provides XMLHttpRequest objects to interact with the server. AJAX requests small pieces of information, not the whole page using HTTP requests.  

AJAX works well with different languages, and it does not require any special server it uses the web page instead.

To know AJAX in detail please visit our website: http://www.roseindia.net/programming-tutorial/Ajax

Following tutorial will help you to learn PHP and AJAX.

In the following example you can see that whenever you enter any alphabet, some suggestions related with that letter will be displayed below the textbox, without refreshing the page, and this is the magic of AJAX. It receives data from the sever in small pieces instead of the whole page.

The example is divided in three parts:

  1. HTML file: In this file you will create the necessary user interface.
  2. PHP file: In this file you will create a PHP file which will enlist all the probable names which will be displayed as suggestion.
  3. AJAX file: This file will be responsible for displaying the dynamic data.

phpAjax.html

<html>

<head>

<script type="text/javascript" src="phpAjax.js"></script>

</head>

<body>

<b><u>Enter Names Below</u></b>

<p/>

<form>

First Name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />

<p>Probable Names are:

</form>

<span id="probable"></span>

</body>

</html>

phpAjax.php

<?php

$a[]="Avishek";

$a[]="Bijay";

$a[]="Chunnilal";

$a[]="Deepak";

$a[]="Dipesh";

$a[]="Elesh";

$a[]="Farha";

$s=$_GET["s"];

if (strlen($s) > 0)

{

$hint="";

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

{

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

{

if ($hint=="")

{

$hint=$a[$i];

}

else

{

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

}

}

}

}

if ($hint == "")

{

$response="Currently Unavailable";

}

else

{

$response=$hint;

}

echo $response;

?>

phpAjax.js

var xmlhttp

function showHint(str)

{

if (str.length==0)

{

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

return;

}

xmlhttp=GetXmlHttpObject();

if (xmlhttp==null)

{

  alert ("Sorry, your browser is compatible!!!");

return;

}

var url="phpAjax.php";

url=url+"?s="+str;

url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged;

xmlhttp.open("GET",url,true);

xmlhttp.send(null);

}

function stateChanged()

{

{

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

}

}

function GetXmlHttpObject()

{

if (window.XMLHttpRequest)

{

// code for IE7+, Firefox, Chrome, Opera, Safari

return new XMLHttpRequest();

}

if (window.ActiveXObject)

{

  // code for IE6, IE5

  return new ActiveXObject("Microsoft.XMLHTTP");

}

return null;

}

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 26, 2009

Ask Questions?    Discuss: PHP Ajax   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com