PDA

View Full Version : Deeper look to Ajax with javaScript script



auriaks
03-08-2010, 08:58 PM
Hi,

I saw a script in w3schools. And Below I have written some comments near functions, but still - there I wrote some questions and you will see them in the script...

Please answer most understandly :) thanks.

Here is the script:


var xmlhttp;

//function ShowUser which generates link (with information) to send to php script.

function showUser(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getuser.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}


//Function StateChanged, is active only when you choose other value in the form, or I AM WRONG????

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}

//And this function is always needed. Maybe to send and get back information?? What does this function? Do I have to use it fully, or I can ommit something?

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}



The real script is HERE (http://www.w3schools.com/PHP/php_ajax_database.asp)

Comment this topic as best you can, because I take a deep interest in every word you will say. Really serious in understanding this. THANKS.

jscheuer1
03-09-2010, 03:04 PM
//function ShowUser which generates link (with information) to send to php script.

Not a link, a URL to be requested from the server. The "information" (an accurate but generic term), is in this case a query string that the server and (presumably) the PHP script treat as GET data.


//Function StateChanged, is active only when you choose other value in the form, or I AM WRONG????

Wrong. The showUser function is only active when the form's select's onchange event fires. StateChanged is only active if the user's browser supports one of the two methods for making a request that are in the GetXmlHttpObject function and that request's readystate changes.


//And this function [sic: GetXmlHttpObject] is always needed. Maybe to send and get back information?? What does this function? Do I have to use it fully, or I can ommit something?

It's used by showUser to determine which of two methods for performing the request a browser may support. Preference is given to the XMLHttpRequest() method, in that if a browser supports both methods, XMLHttpRequest is the one that will be used. There are at least two others that are omitted here, but only one of those may be important, that for the icebrowser. Omitting anything material from GetXmlHttpObject will only limit the number of browsers supported. GetXmlHttpObject could be rewritten to be more efficient. But it probably would then be even less clear what it's doing.

The request object generated by GetXmlHttpObject is modified by the showUser function and then used by showUser to send information to the server requesting a page (in this case a PHP script, that presumably will return the desired information as the request object's responseText, an HTML string). If the request object's readystate changes (which it will do several times during the normal execution of an eventually successful request), showUser has configured it (the request object) to fire stateChanged, which in turn tests the readystate to see if it is 4 (complete). If so the HTML string returned as the responseText is placed in the txtHint element.

auriaks
03-09-2010, 05:39 PM
That was really helpful, thanks.