AJAX(XMLHTTPRequest) Help for Newbies
Hello friends
Here is a code if you want to send some data to the server using AJAX.
AJAX is a revolution in the technological world of Web Applications.
Those who want to send data to the server side language like PHP for
addition/editing/deletion.
you may nedd this(this is for addition of data):
<script language="JavaScript">
var requester;
function loadGrid()
{
if (requester != null && requester.readyState != 0 && requester.readyState != 4)
{
requester.abort();
}
try
{
requester = new XMLHttpRequest();
//alert("Mozilla");
if (requester.overrideMimeType) {
requester.overrideMimeType('text/xml');
}
//above try block creates a requester object for Mozilla
}
catch (error)
{
try
{
requester = new ActiveXObject("Microsoft.XMLHTTP");
//alert("IE");
//above try block creates a requester object for IE as both have independent implementation
}
catch (error)
{
requester = null;
return false;
}
}
//alert(document.fCreate.pName.value);
var str = document.fCreate.pName.name+"="+document.fCreate.pName.value+"&";
str+= document.fCreate.pAddress.name+"="+document.fCreate.pAddress.value+"&";
str+= document.fCreate.pCountry.name+"="+document.fCreate.pCountry.value+"&";
str+= document.fCreate.pPrivilege.name+"="+document.fCreate.pPrivilege.value+"";
//alert(str);
//above str variable contains the value that needs to be posted in the
//format=>"pName="Ritesh"&pAddress="your address"
//&pCountry="India&pPrivilege="Admin"
//Note: u have to create this str acc to ur form name and value pairs
requester.open("POST", "process.php",true);
//Here is a URL(process.php) PHP lovers shud know how to handle POST
//variables and use to communicate with Database
requester.onreadystatechange = onreadystatechangeAdd;
requester.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
// The above line needs to be added because of the POST method
//because data send in the form of POST method doesn't come in the
// $_POST[] array in PHP. The additional line sends the POSTed data to
//be retrieved in the POST array
requester.send(str);
//In this string has to be passed in the form of name=value pairs
// separated by '&' as shown above
return true;
}
/* Execute the action which has been associated with the completion of this object */
function onreadystatechangeAdd()
{
// If XMLHTTP object has finished retrieving the data
//alert(requester.readyState);
if (requester.readyState == 4)
{
// If the data was retrieved successfully
try
{
if (requester.status == 200)
{
obj.refresh();
//Ur process.php file do the addition of record u just to need to refresh
// the grid to accomodate the changes
}
// IE returns a status code of 0 on some occasions, so ignore this case
else if (requester.status != 0)
{
alert("There was an error while retrieving the URL: " + requester.statusText);
}
}
catch (error)
{
}
}
return true;
}
</script>
<!-- AJAX Request Script Ends -->
You need to call the loadGrid() function on the appropriate event
and u have to create a file process.php doing the addition of records
through sql queries
That's all u have to do.
Ask if got any problem.....
Cheers :)
Here is a code if you want to send some data to the server using AJAX.
AJAX is a revolution in the technological world of Web Applications.
Those who want to send data to the server side language like PHP for
addition/editing/deletion.
you may nedd this(this is for addition of data):
<script language="JavaScript">
var requester;
function loadGrid()
{
if (requester != null && requester.readyState != 0 && requester.readyState != 4)
{
requester.abort();
}
try
{
requester = new XMLHttpRequest();
//alert("Mozilla");
if (requester.overrideMimeType) {
requester.overrideMimeType('text/xml');
}
//above try block creates a requester object for Mozilla
}
catch (error)
{
try
{
requester = new ActiveXObject("Microsoft.XMLHTTP");
//alert("IE");
//above try block creates a requester object for IE as both have independent implementation
}
catch (error)
{
requester = null;
return false;
}
}
//alert(document.fCreate.pName.value);
var str = document.fCreate.pName.name+"="+document.fCreate.pName.value+"&";
str+= document.fCreate.pAddress.name+"="+document.fCreate.pAddress.value+"&";
str+= document.fCreate.pCountry.name+"="+document.fCreate.pCountry.value+"&";
str+= document.fCreate.pPrivilege.name+"="+document.fCreate.pPrivilege.value+"";
//alert(str);
//above str variable contains the value that needs to be posted in the
//format=>"pName="Ritesh"&pAddress="your address"
//&pCountry="India&pPrivilege="Admin"
//Note: u have to create this str acc to ur form name and value pairs
requester.open("POST", "process.php",true);
//Here is a URL(process.php) PHP lovers shud know how to handle POST
//variables and use to communicate with Database
requester.onreadystatechange = onreadystatechangeAdd;
requester.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
// The above line needs to be added because of the POST method
//because data send in the form of POST method doesn't come in the
// $_POST[] array in PHP. The additional line sends the POSTed data to
//be retrieved in the POST array
requester.send(str);
//In this string has to be passed in the form of name=value pairs
// separated by '&' as shown above
return true;
}
/* Execute the action which has been associated with the completion of this object */
function onreadystatechangeAdd()
{
// If XMLHTTP object has finished retrieving the data
//alert(requester.readyState);
if (requester.readyState == 4)
{
// If the data was retrieved successfully
try
{
if (requester.status == 200)
{
obj.refresh();
//Ur process.php file do the addition of record u just to need to refresh
// the grid to accomodate the changes
}
// IE returns a status code of 0 on some occasions, so ignore this case
else if (requester.status != 0)
{
alert("There was an error while retrieving the URL: " + requester.statusText);
}
}
catch (error)
{
}
}
return true;
}
</script>
<!-- AJAX Request Script Ends -->
You need to call the loadGrid() function on the appropriate event
and u have to create a file process.php doing the addition of records
through sql queries
That's all u have to do.
Ask if got any problem.....
Cheers :)
Ritesh Jagga
November 23,