PHP AJAX Introduction


 

PHP AJAX Introduction

In this current tutorial we will study how to integrate a simple PHP file with AJAX, how to put some AJAX affect in a PHP file. In AJAX JavaScript can directly communicate with the web server with the help of XMLHttpRequest object and without reloading the page, this makes AJAX more attractive and effective.

In this current tutorial we will study how to integrate a simple PHP file with AJAX, how to put some AJAX affect in a PHP file. In AJAX JavaScript can directly communicate with the web server with the help of XMLHttpRequest object and without reloading the page, this makes AJAX more attractive and effective.

PHP-AJAX-Introduction

In this current tutorial we will study how to integrate a simple PHP file with AJAX, how to put some AJAX affect in a PHP file. But first of all let's discuss what is AJAX and how it works?

AJAX stands for Asynchronous JavaScript And XML.

AJAX is not a new programming language but it is a combination of few web standards like JavaScript, XML, CSS, HTML.

In AJAX JavaScript can directly communicate with the web server with the help of XMLHttpRequest object and without reloading the page, this makes AJAX more attractive and effective.

AJAX uses Asynchronous data transfer between the browser and the server, allowing web pages to reload in a small fragment of web page instead of reloading the whole page.

 

In the following example we will try to display dynamic content which is fetched from the database dynamically and displays on the screen.

First of all we need to create a table in mysql, just copy and paste the following code in mysql console screen or on any GUI,

create database <database name>;
use <database name>;

create table <table name>
(city_id int primary key auto_increment, city_name varchar(50));

select * from <table name>;

insert into <table name>
values('','<city name>');

Note: Please replace the code within <> bracket with appropriate name.

After creation of table we will need three different files html file, JavaScript file, and PHP file.

php-ajax.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<TITLE> PHP Ajax Example </TITLE>

<script type="text/javascript" src="php-ajax.js">

</script>

</HEAD>

<BODY>

<form name="firstAjax">

City: <input type="text" id="city" onkeyup=" dispCity(this.value)"/>

</form>

<br/>

Suggestions:<span id="cityname"></span>

</BODY>

</HTML>

 

php-ajax.php

0

<?php

$con=mysql_connect('localhost','root','');

if(!$con){

1

die("Can not connect". mysql_error());

}

2

mysql_select_db('ajax',$con);

$sql="select * from city";

3

$result=mysql_query($sql,$con);

4

$c=$_GET['c'];

$a=array();

$city="";

5

if(strlen($c)>0){

$city="";

6

$i=0;

while($row=mysql_fetch_array($result)){

7

array_push($a,$row['city_name']);

}

8

}

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

9

{

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

0

if($city==""){

$city=$a[$i];

1

}

else

{

2

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

}

3

}

if($city=="")

4

{

$res="No suggestion";

}

5

else

{

$res=$city;

6

}

}

echo $res;

7

?>

 

php-ajax.js

8

var xmlhttp

function dispCity(str)

9

{

if (str.length==0)

{

0

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

return;

}

1

xmlhttp=GetXmlHttpObject();

if (xmlhttp==null)

{

2

alert ("Your browser does not support XMLHTTP!");

return;

}

3

var url="php-ajax.php";

url=url+"?c="+str;

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

4

xmlhttp.onreadystatechange=stateChanged;

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

xmlhttp.send(null);

5

}

function stateChanged()

6

{

if (xmlhttp.readyState==4)

{

7

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

}

}

8

function GetXmlHttpObject()

{

9

if (window.XMLHttpRequest)

{

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

0

return new XMLHttpRequest();

}

if (window.ActiveXObject)

1

{

// code for IE6, IE5

return new ActiveXObject("Microsoft.XMLHTTP");

2

}

return null;

}

3

 

Output:

Output of Ajax

4

 

Ads