Professional Documents
Culture Documents
// Get the value of the "name" field and assign it to a variable called name
var name = document.getElementById("name").value;
<form>
<p>State: <input type = "text" name = "state" size = "25"
onChange = "callServer();" /> </p>
</form>
function callServer() {
// Get city and state from web form
var city = document.getElementById("city").value;
var state = document.getElementById("state").value;
The escape() method is used to escape any characters like spaces that cannot be sent
as clear text correctly.
function callServer() {
// Get city and state from web form
var city = document.getElementById("city").value;
var state = document.getElementById("state").value;
We have created a simple AJAX application that illustrates the basic concepts. This
application is a Pizza Ordering Form. There are various inputs in this ordering form,
the first of which is the phone number of the customer. The Pizza Company keeps a
database of all the customers who have ordered before. As soon as the customer types
in his / her phone number a call is made to the server to search the database for that
phone number and extract all the relevant information on that client. That information
is then used to auto fill the form. Here is the table that has been created in the
database:
Customers
Last First
Phone Street City State Zip
Name Name
407-896- 123
Mouse Mickey Orlando FL 32801
0001 Cheddar St.
407-896- 387 Eider
Duck Donald Orlando FL 32801
0002 Dn.
407-896- 482 Brie
Mouse Minnie Orlando FL 32801
0003 Ln.
Here is the Pizza Ordering Form. The JavaScript code that forms the core of the
AJAX application and the PHP code to extract data from the database are not robust
nor secure. We have taken the happy path in this example to illustrate how AJAX
works and not clutter the application with error handling code.
XML Requests to the Server
XML is neither necessary nor required to make requests to the server. In fact it is less
efficient than sending data in plain text as name-value pairs. The only compelling
reason to use the XML format in sending requests is that the script on the server side
does not accept data in any other format. Here is an example of how to send a request
in XML format:
Let us say that the server only accepts a person's name in this format:
<name>
<firstName> Mickey </firstName>
<lastName> Mouse </lastName>
</name>
function callServer()
{
// Get the first and last name from the form
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
While it is easy to send a request as name-value pairs to the server, no matter in what
format the response comes back it has to be parsed on the client side. It makes sense
to send the response in as unambiguous way as possible. This where the strength of
XML lies. If the response came back in the following format you would have no
problem understanding what it meant:
<students>
<student>
<name> Mickey Mouse </name>
<major> Dance </major>
<gpa> 3.5 </gpa>
</student>
<student>
<name> Donald Duck </name>
<major> Song </major>
<gpa> 3.2 </gpa>
</student>
</students>
There are two ways in which you can treat the XML response from the server:
If you treat the XML response as plain text your code in the function updatePage() to
accept the response would be of the form:
var response = request.responseText;
where response would be in the form of a long string with no spacings like so:
<students><student><name>Mickey Mouse</name>
<major> Dance </major><gpa>3.5</gpa></student>
<student><name>Donald Duck</name><major>Song</major>
<gpa>3.2</gpa></student></students>
The string has been split into lines for readability. You would then parse the string
using the various String functions in JavaScript like split() and extract the
information.
On the other hand if you treat the XML response as DOM document object then
JavaScript provides utilities to parse the response more efficiently. The following
example shows how:
function updatePage()
{
if ((request.readyState == 4) && (request.status == 200))
{
var xmlDoc = request.responseXML;
Google Maps
Google Suggest
Yahoo Maps
References
Mastering AJAX: Part 2: Make Asynchronous requests with JavaScript and AJAX by
Brett McLaughlin.
Mastering AJAX: Part 4: Exploiting DOM for Web Response by Brett McLaughlin.
Mastering AJAX: Part 9: Using the Google Ajax Search API by Brett McLaughlin.
Mastering AJAX: Part 10: Using JSON for Data Transfer by Brett McLaughlin.
Mastering AJAX: Part 11: JSON on the Server Side by Brett McLaughlin.