Bulk updates help keep our Ajax applications running more efficiently by keeping the number of database requests down. This is because large amounts of data can be sent as an array, XML, or JSON rather than separate XHRs. This data can be deleted from, saved to, or inserted into the database as one SQL query. Imagine that we have a number of items we want to save to the database. These items could be user attributes, for example, such as names, descriptions, and so on. In most examples, requests are made with each of these attributes as separate key/value pairs. This solution works fine for simple XHRs that need to send only small amounts of data, but if we wanted to send data for multiple users, it would be much more efficient to send it as a bulk update. Let's look at three different ways that we could send bulk data to the server via an XHR.
As you can see, we have a name and a description key with corresponding values. As an example, we will act as if we are going to insert this data into a database with a method, which simply makes queries directly on our database based on the HTTP queries that were sent to it. We will first need to abstract our serviceConnector.php file a bit in order to simply send the HTTP query data directly to our handling method, rather than send the data as we did in the previous chapter.
<?php header("Content-Type: application/xml; charset=UTF-8"); require_once("classes/UserManager.class.php"); $o = new $_GET['object'](); echo $o->$_GET['method']( $_GET ); ?>
We are simply passing the query data as a whole rather than splitting it or sending a specific key. The following is an example of how we would write the method that will make the connection between our Ajax engine and our server-side object.
This option is very easy to manage and, even though it is not the best solution, it can provide us with some powerful ways of manipulating bulk data.
Sending XML or JSON is slightly different from sending a simple array because the data can be much more complex and therefore achieve results on a larger scale. In this section, we will focus on sending XML as our request data and cover some of the possibilities that can be reached with this approach. As we have seen throughout the samples in this book, XML is a flexible language that can be used to encapsulate static or dynamic data as any custom structure that we define. This, of course, leaves a lot of room for interpretation and allows complete flexibility in our code, which enables us to exchange complex data types between different languages. This is why XML lends much more power to our requests than passing a simple array or other custom character-delimited lists. The following is an example of a simple query that can be sent from any application to pass data as XML to be used on our server side based on the object and method specified.
serviceConnector.php?object=DatabaseManager&method=parseXML&xml=<names> <name>Kris Hadlock</name></names>
After the sample is set on your server, you can simply paste this query on the end of your URL and you will receive the response that we will now construct.
This method will have been called based on the query that took place via the serviceConnector. In this method, I am simply showing you how to load the XML and parse the data we have sent to it. Of course, this method can be much more complex because it can receive XML, save, insert, or delete node values in the database, and so on. After the data has been used to manipulate the database, we can take the values and respond to the requestor.