Wednesday, January 31, 2007

Getting started with Ajax

Ok so you want to get into the basics of Ajax? Maybe you've heard of it, maybe not.
If you haven't, basically it's a way for javascript to load information into a page without the page refreshing. This looks more professional and can be extremely useful in a database driven website, such as the kind I would prefer to make in PHP and MySQL

In my case, I'd use Ajax to call a seperate PHP page that would give back info, such as maybe database info or info from a file. the latter would be less preferable as if the site were dynamic with many users changing a file mid-read could case problems, especially on Windows servers. {I'm guessing here but I like to be cautious}

Anyway here's an example of how I would use Ajax:

I'm presuming you've used PHP + MySQL before but if not read on anyway and you might find some of this interesting.
I'd have one PHP or even HTML file with a form, some textareas or list boxes or whatever. When you change the text in one area, or select something from a listbox, it would run a seperate PHP file. This PHP file would be setup to connect to my database and get the info needed based on a given parameter, eg.

this would run the PHP which would go and get the info from the database for id 12
so then I could populate a textbox or another listbox on my main HTML page and voila - you've got a professional, dynamic, interactive, database-driven application!!!!! even typing those words makes me squirm but that's basically what you're doing

Now obviously these are just the basics but Ajax can prove very powerful.

You'll need the code for doing what I said above, and I found a good guide to getting started here

No comments: