Uncategorized

Simple Ajax in Php


My this blog is out of my  road map, Just to give an overview on ajax with php. Im this blog I am going to use the simple ajax example no more theory just a simple way to implement the ajax in php page.

However there are so many ways  to integrate the ajax with php, i.e. jquery , js, jason , xml but I am explaining a simple ajax example.

To Implement an ajax on the php page all you need to just a 3 JavaScript function in js file, 2 php pages one for the front end and one which execute yur ajax queries on server.

Ajax is all about to create an xmlhttprequest to the server from client browser, what it does only the specific piece of the page content goes to the server as xmlhttprequest which renders and get back to page.

Follow the steps.

what we need, 2 php page and one js file.

this is our front page,

(1)<div id=”order”></div>

<a id=’myid’ onclick=’ GetOrderDetail(this.id)’>Call Ajax</a> // this will call your js function, which send request to your backend page , page1.php, where you can do your server side programing,

1) Now come to javascript, which create the main handling in this, Add this method to JavaScript file.

function GetOrderDetail(id) // you are going to call this method from your page, you can send any parameter
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert(“Your Browser Donst Support Ajax”);
return;
}

var url=”page1.php?id=”+id;// this is your back end page, where u render what u send from page
xmlHttp.onreadystatechange=Orderdetail_Change;  you need to change the methoed name which is below
xmlHttp.open(“Get”,url,true); // nothing to change

xmlHttp.send(null);

}
now what you return for your page comes to this method

function Orderdetail_Change()
{

if (xmlHttp.readyState==4)
{

var _a = xmlHttp.responseText; // this your whole response from the page
document.getElementById(“order”).innerHTML=xmlHttp.responseText; //create any div where u show your data, order is the div name

}
}

 

paste as it is. no change in this

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}

thats it,  now come to third and last step, which is to create our backend page,

Sample , this is our back page.

Page1.php

<?php

Echo request.querystring[“id”];

Echo “this is return from ajax”;

?>

 

This is only a reference, Kindly check the syntax as I am in middle of the sharpont project , and making pause on that and wrote this blog, if the is any syntax pro , so I apologize,  my main reason is to provide easiest way to ajax, I will also make this ajax trick to share point 2010 website.

 

 

 

Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s