At the client browser, what Ajax does is that it calls a file at the server. This file at the server has to be an executable file. The name of this file is part of the URL used by the Ajax at the client. The function of this file is to look for the information requested from the server and send back to the client. The aim of this article is to show you how to send back the required text to the client browser.
I use the Perl and PHP languages for illustration.
You need basic knowledge in Ajax and Perl or PHP in order to understand this article.
To handle Ajax at the server, you do not need to learn any new standard or technology. All you need to know is how to send back information from the server to the client.
Type of data
The information sorted may be text in a text file, a whole text file, text in a database or an XML file. For simplicity we shall assume that the information sorted is just some text (a paragraph) residing somewhere in the server. It is the responsibility of this executable file to look for the required text in the server. Since the aim of this article is to see how the information is sent back, for simplicity again we shall not see how the executable file looks for the information in the server.
The Connection and Sending Information back
Once the connection is established between the client browser and the server (executable file), it is like there is a stream between the client and the browser. Any information that the executable file at the server sends to its standard output goes but to the stream and back to the client browser (not to the computer monitor of the server). So in the case of Perl, all you need to do to send back the information is to type
where $string is a string variable containing the required text obtained from the server. In the case of PHP, all you need to do to send back the information is to type,
where $string is a string variable containing the required text obtained from the server.
The Query String
The query string is the data send by Ajax from the client browser. With the GET method, all the information after the ‘?’ character in the URL, is the query string. With the POST method, the query string is sent as the argument of the Ajax object send() method.
The executable file at the server uses the information in the query string to look for a particular text in the server. If there is no query string, the executable file should send a default text.
An example of a query string is:
This means that first name is John and last name is Smith.
Illustration with Perl
Let us consider a simple Perl script called ajaxPerlSvr.pl at the server. This is the executable file at the server. This is the file name in the URL of Ajax at the client.
Assume that Ajax at the client sent the above query string. Let us say, the aim of the file, ajaxPerlSvr.pl at the server, is to extract the first name, John form the query string sent to the server, and then send back “John” to the client browser, as the requested text from the server. In practice, the “John” will be used to obtain (search) a particular text in the server and have the text obtained, sent to the client.
This is the content of the Perl file:
$query = new CGI;
$value = $query->param(‘fname’);
print “Content-Type: text/html\n\n”;
Perl has a module called the CGI module. This module has functions that receive the query string. The first statement above imports the CGI module into your program. This module is actually a class. It has properties and methods to manipulate the query string. The next statement in the script creates an object from the class. The name of the object is $query. This object has your query string.
The statement after, extracts the first name, “John” from the object, and assigns it to the variable, $value. This statement needs some more explanation: Remember that the query string is made up of name/value pairs. The Perl CGI class has a method called, param(). If the argument of param() is the name of a name/value pair, then the param() method will return the value.
‘fname’ and ‘John’ form a name/value pair in our query string. So if the argument of the param() method is ‘fname’, the param() method will return, ‘John’. In our case, the object that uses the param() method is $query. In order to call the method, param() of the object, $query, you need to have “->” between the object and the method. The third statement should now be clear.
Once you have the required text in the Perl program, all you have to do is to use the print command to the send the text back to the client. Since there is a connection (stream) with the executable file, the text goes to the client instead of the server monitor. However, before you send the required text, you must first send the following information:
This explains why you have the first print statement. The required text is in the variable, $value. The second print statement sends the required text.
Illustration with PHP
There are two PHP programs below. Each of these programs does exactly what the Perl program (code segment) above does. With PHP, if Ajax used POST to send the query string, then at the server, PHP will have the name/value pairs of the query string in an associative array, called $_POST. This is an internal PHP array and it is globally accessible. An associative array consists of keys with corresponding values. For the query string name/value pairs, the $_POST array keys are names, while the corresponding array values are query string values. The following PHP code is for the POST method:
$value = $_POST[“fname”];
This is a PHP executable file whose name has to be in the URL of the Ajax code at the client. There are two lines in the code. The first line uses the name, “fname” as key of the $_POST associative array to obtain the value, “John”. This value is assigned to the variable, $value. Remember, the associative array will always hold the query string sent. The second line in the code sends the value “John” into the stream of the connection.
The following code does the same thing, but for the GET method. Here PHP has the $_GET associative array to hold the name/value pairs of the query string, sent by Ajax, using the GET method.
$value = $_GET[“fname”];
At the server there is no new standard or technology for Ajax. All you have to know is how a language gets the data from the query string sent and how it sends text (any string) back into a connection stream initiated from the client. You can use any language for this: C++, Borland Delphi, Visual Basic.net etc. I have used only perl and PHP for illustration.
To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (use menu if available):
Making Ajax Request
Receiving Response to Ajax Request
Ajax at the Server