When we build Web 2.0 applications we have to deal, eventually, with some way to update our pages "on the fly" and one of the best solutions I found is to combine Prototype, the javascript framework (http://www.prototypejs.org/), and AJAX.
As this type of solution is oriented to advanced developers I'm not going to explain here the concepts, how to add prototype or things like these. If you are new to some concepts just Google them.
Ok, let's begin.
First we are going to use a prototype class named AJAX with the following syntax:
JAVASCRIPT:
-
var sUrl = "./mydata.php";
-
-
new Ajax.Request(sUrl, {
-
method: 'get',
-
parameters: { year: '2009', header: 'Months' },
-
onSuccess: function(transport) {
-
if ( 200 == transport.status ) {
-
var myData = transport.responseText.evalJSON(true);
-
}
-
}
-
});
Our explanation is the following:
- We are using the method request of our Ajax prototype class
- sUrl is a variable containing the url of the external file we are linking to, remember that this URL can’t begin with http because of the same-domain policy of Ajax even if the URL points internally
- In the method we can use 'get' or 'post' off course
- Parameters argument is an object that can contain any number of property/value pairs
- onSuccess defines a function to receive the results of our file, as we can send any kind of content here we are using JSON and I recommend you to use JSON to move great volumes of content, use the json_encode function in PHP to prepare your data.
As you see it's really simple to make an AJAX request using prototype.












