Insomnia can really help with productivity
Oct 08, 2006

It's nearly 6 am on Sunday, October 8th. For whatever reason, I can't sleep. Instead, I've spend the past 3 or so hours writing AJAX code. If you're not a programmer, there's a good chance this is the last sentence of this post that you'll want to read. Don't say I didn't warn you. I'm working on a project that pairs an AJAX front-end with a PHP back-end. To make things even more complicated, the content for the site is stored both in a MySQL database and in XML files. The plus side is that the complexity of the project has pretty much forced me into improving my coding style, my college advisor would be proud. It's also making it much more interesting and requiring me to learn a lot just to get it done. In the time since I gave up on trying to get to sleep I've managed to solve a major problem that was preventing me from making any more progress. The way the site works, there is an XML document that defines the structure of the data that will be displayed. This XML file is read in and converted into a PHP object (actually it's one PHP object with an array of other objects, each of which has it's own array of objects...). Once the structure is known, obviously we need to drop the data into it. The first step to doing this is an asynchronous HTTP request to a PHP script that then has to have access to one of the child objects of the main object (as well as all of its respective children). This is the part that was throwing me off as I can't just throw the PHP object into the arguments of a javascript function. Eventually I just passed the name of the XML file along to the script and recreated the entire set of objects. I'll probably try and find a more efficient way of doing that eventually... (Actually, it's just now occurred to me that I can probably serialize() the relevant objects, pass them that way, and then just unserialize() them in the PHP script. I'll have to play around with that.) Once the object set has been re-created in the PHP script, it is then able to pull the needed data from a MySQL database and send it back. Unfortunately, the data that gets sent back needs to go into three different places, so the response has to be in XML. This is where I'm currently stuck. I can format the response as valid XML with no problem, however once I do that and echo it back, the javascript doesn't actually seem to recognize that it's XML. I can only get at the data through the responseText variable, and not the responseXML one. This would be fine, except that I apparently can't use getElementByTagName() on responseText which makes it difficult to get at the data in any useful way. So I need to figure out how to get javascript to actually interpret the response as XML. This is where I'm stuck right now, and again, until I figure this out, I can't make any more progress. The really annoying part is that this latest problem is the last hard part. Once I figure this out, the rest of the project will be a breeze, and should only take another couple of hours (at least to get it demo-able, there are a few other complex features I need to add before it's actually ready, but they don't affect the functionality so much as the interface). Hopefully I'll be able to figure this out (or some helpful soul will be able to give me a hint) and get the project finished by the end of next week. For now, I think I'm going to give sleeping another go.
blog comments powered by Disqus