Home » Archives » May 2010
PHP and AJAX
May 26, 2010Ajax (shorthand for asynchronous JavaScript and XML[1]) is a group of interrelated web development techniques used on the client-side to create interactive web applications.
-we use AJAX for the dynamic controls of our web applications…so this is morely on the client side of the application..
and the server side language is PHP..
so combining the two we can build a several web applications…isn’t it?..
so that is why…
i have a very simple program demonstrating the PHP and AJAX coding..
1. assuming we have file test.php
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Reverse using Ajax</title>
</head>
<script language=”javascript”>
function doReverse(){
var str=document.getElementById(”txtStr”).value;
var httpObject=getHTTPObject();
if(httpObject!=null){
httpObject.open(”GET”,”reverse.php?str=”+str,true);
httpObject.send(null);
httpObject.onreadystatechange=
function updateReverse(){
var responseStr;
if(httpObject.readyState==4){
responseStr=httpObject.responseText;
document.getElementById(”txtReverse”).value=responseStr;
document.getElementById(”txtStr”).value=responseStr;
}
}
}
}
function getHTTPObject(){
if(window.ActiveXObject)
return new ActiveXObject(”Microsoft.XMLHTTP”);
else if(window.XMLHttpRequest)
return new XMLHttpRequest();
else{
alert(”Your browser does not support AJAX”);
return null;
}
}
</script>
<body>
<table width=”392″ border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”184″>String:</td>
<td width=”208″><input id=”txtStr” type=”text” size=”40″ /></td>
</tr>
<tr>
<td>String in Reverse:</td>
<td><input name=”text” type=”text” id=”txtReverse” size=”40″ /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type=”button” name=”cmdReverse” value=”Reverse” onclick=”doReverse();” /></td>
</tr>
</table>
</body>
</html>
2.we have also file reverse.php
<?php
$str=$_GET[”str”];
echo strrev($str);
?>
so this is the very simple way implementing AJAX in PHP..
hope it helps..




