My first AJAX!

It is an Enchanted Device Generator for Ars Magica (4th Edition). It’s built using PHP and Sajax which is a free AJAX toolkit.

It’s been quite an enlightening experiment. The buzz and hype generated around AJAX is quite something, so I thought I’d give it a shot. I started with an idea, a simple script that takes in a few values and does some calculations and presents the result to the user. But, I wanted the user to be able to tweak the values and see the results immediately. That sounds like something AJAX is perfect for.

So the first thing I learnt as I started building the script and using Sajax was JavaScript. I used to avoid JavaScript like the plague. I know you can do some funky stuff but in my past experience, JavaScript slows down the user experience and can be very flaky across browsers. Still, what I learned was that I could have easily built my script using just JavaScript.

Yet AJAX would allow me to offload work from the browser to the server. Great, I thought, now I can do the calculations using PHP and just do all the text stuff using JavaScript. The snag I came across was that Sajax treated PHP as an intermediary interface. For example global variables defined in PHP were not accessible when a PHP function was called from JavaScript. Not alone that, only strings could be passed back from PHP. Rich types like arrays are not currently possible. I solved this by doing the calculations once, putting all the values in a string and then passing that string around when calling other PHP functions through Sajax.

It works now and I’m happy enough. One thing I noticed while I was working away, is that I started thinking about the script more as a User Interface than a webpage. I guess that’s what the hype is really about. All this Web 2.0 silliness. On the other hand, I found it hard to separate the HTML and CSS code and design from the JavaScript and PHP coding. If you look at the script, it’s very bare. I guess if you treat it like a UI, you’d do your “prototype” first and then add code.

Though I’ve come to love PHP, I have my reservations. While it’s a powerful feature to mix html and code, it always bugged me that you can write code that essentially modifies and generates code (in this case HTML, CSS and JavaScript). I guess I’m old fart but that use to be a no-no in the land of C and ASM (a land I know better than I want). It makes the code unreadable and un-maintainable. And I think often PHP is a little unreadable unless you have a syntax-aware editor or layout your code very well. I’m one of those ‘hardened programmers’ that prefer basic text editors over IDEs (though I’ve been softened lately by JEdit). Still the idea of using AJAX to create entire UI-like interfaces is a little scary. You really need to think about design of your code to make it maintainable. Still, I like the potential of AJAX and the possible applications on webpage. On the other hand for small ideas, I could just use some JavaScript.

Related Posts: