A great book for wordpress theme design

Posted by Andy Bailey at 17 June, 2008, 10:40 am
22


I bought a book recently after seeing it mentioned in the WP dashboard news items, it’s called Wordpress Theme Design by Tessa Blakeley Silver and although a lot of the information regarding the functions of wordpress can be found at the codex, the book gives you so much more like the process gone through to design a theme from scratch.

I do quite a lot of sites in wordpress now and find it much easier to provide a client with a website that can be updated so easily, a few plugins here and there and you can provide a website that does all and more that’s needed.

Usually I get a standard theme and cut it up a bit and change the css to give me my site, or create in photoshop and send off for wordpressing but lately due to some more custom design requests I’ve received and the always dependable mistakes that freelancers can make I have wanted to do the whole thing myself from scratch..

I’ve downloaded a tutorial before about WP theme design from small potato before it was taken over and that was pretty darn useful but was made a little while ago so it wasn’t 2.5 specific.

That’s why I like what Tessa has put her book, it takes you step by step through the process of creating a non-blog wordpress blog, from sketch to WP core code. It’s made a big difference to my initial prototyping of concept sites and has really opened my eyes to what you can do with a bit of manipulation of the wordpress template tags. I’m even working on a magazine style front page for here so I can have a featured post for contests that stays up while it’s open for entries as well as excerpts to the regular posts..

You can buy the book printed or get it as pdf for a bit cheaper (I printed mine out from pdf on someone elses printer lol!) at packet publishing

Popularity: 6% [?]

Category : Reviews | Wordpress | books

Beta testing CommentLuv 1.0

Posted by Andy Bailey at 12 April, 2008, 1:32 am
32

I have finally completed the code for CommentLuv 1.0

phew! I had a mammoth amount of work to do this week but I got a few hours out of nowhere and I’ve managed to get it stable with these features:

  • Options page where you can change
    • Text displayed under the comment box
    • Text used within the comment
      can choose to show name , site and last blog post
    • Choose to enable CommentLuv by default
    • Add styling to last blog post text
  • Uses an external script to do the fetching
    This means that the magpie library included with wordpress doesn’t get used. Instead I try to curl a page I host on my dedicated server which outputs the last post link. If curl isn’t enabled, it tries to fetch it another way. This way I can keep the most up to date fetching routine on my server and when I update that, all people using commentluv will use the most up to date routine

I will be adding AJAX to it later but for now it is working quite stable, I need to run it on here for a few days to make sure it can behave well and then I’ll package it for a 1.0 release and put ajax in for the 1.5 release…

please feel free to make a comment and test it out for me…. thanks!

[edit]… You can download the beta testing version by clicking here Just extract and overwrite the existing file, there is an options page in the dashboard under “settings” in WP2.5

Popularity: 4% [?]

Category : Blog Tools | Code | PHP

CommentLuv Ajax Preview, now platform independent !

Posted by Andy Bailey at 7 April, 2008, 11:22 pm
31

I spent a large part of last weekend coding some changes to CommentLuv, well, changing the way it can be called and how it displays posts and such. Here’s a little preview of what sort of thing it can do on a dummy form, let me know if it can’t pick up your last post or if you experience any issues with it…


This is running as just javascript and jquery without using any of the wordpress core functions and it should work on standard forms on sites that don’t have PHP enabled. I just need to tidy up the code and package it in an easy to install way and it’ll open up CommentLuv to many more platforms…yey!

I’m not 100% sure yet but this could mean a blogspot commentluv plugin, I’m trying some experiments now and so far the results are promising…

Popularity: 6% [?]

Category : Blog News | Blog Tools | Code | PHP | Social Networking | ajax

Beginners AJAX Tutorial Series Part 5

Posted by Andy Bailey at 4 January, 2008, 12:17 pm
19

AJAX Avenue

This is part 5 of the AJAX Tutorial Series.
You can see the other Beginners AJAX Tutorials here.

Hurrah! this tutorial will actually download some data (yey!) We will use everything we have learned so far and add just a little bit that will allow us to download data from the server and put it somewhere on the page without a refresh. (I know, you must have goosebumps!) :-)

XMLHttpRequestObject.send(null);

We need this command to initiate the object send. We created an object, created a place for the data to go, we created a routine to wait patiently for the object to be ready and sent the object to the data source using the above command. The patient routine for the waiting will take the response to the data source and put it in the div we created in the HTML of the page.

We can replace the bit from the last bit of complete code (on part 4) with this command and instead of it saying it's ready to download, it will actually do the fetching for the obj.innerHTML=XMLHttpRequestObject.responseText; which is called when the readyState and Status are ok.

HTML:
  1.     <title>FiddyP AJAX Tutorial Series Part 5</title>
  2.     <script language = "javascript">
  3.       var XMLHttpRequestObject = false;
  4.       if (window.XMLHttpRequest) {
  5.         XMLHttpRequestObject = new XMLHttpRequest();
  6.       } else if (window.ActiveXObject) {
  7.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  8.       }
  9.       function getData(dataSource, divID)
  10.       {
  11.         if(XMLHttpRequestObject) {
  12.           var obj = document.getElementById(divID);
  13.           XMLHttpRequestObject.open("GET", dataSource);
  14.           XMLHttpRequestObject.onreadystatechange = function()
  15.           {
  16.             if (XMLHttpRequestObject.readyState == 4 &&
  17.               XMLHttpRequestObject.status == 200) {
  18.                 obj.innerHTML=XMLHttpRequestObject.responseText;
  19.             }
  20.           }
  21.        XMLHttpRequestObject.send(null);
  22.         }
  23.       }
  24.     </script>
  25.   </head>
  26.     <H1>Fetching data with Ajax</H1>
  27.     <form>
  28.       <input type = "button" value = "Display Message"
  29.         onclick = "getData('oranges.txt', 'targetDiv')">
  30.     </form>
  31.     <div id="targetDiv">
  32.       <p>The fetched data will go here.</p>
  33.     </div>
  34.   </body>
  35. </html>

I'll try and make this code easier to understand with a picture using pretty colours! :-)
Part 5 AJAX picture

The blue bit gets called when the user clicks the button, if the object was successfully created then create a routine that 'listens' to the objects readyState and status. Then the XMLHttpRequestObject.send(null); command does the action and when that goes ahead and activates XMLHttpRequestObject.open("GET", dataSource); which goes off and gets the data pointed to by dataSource which of course changes the objects status and readyState. When it is at what we want, we inject the responseText to the div pointed to by divID.

Simple! :-/

You can see it in action here

Exciting huh? LOL . It'll display anything that is in the oranges.txt file, not much use yet but at least we know we can fetch data!

What if, instead of displaying the contents of a text file, we used a php script to 'echo' out something?.. replace oranges.txt with this code..

PHP:
  1. echo "these are some oranges created by php!";
  2. echo "<br/>ooooooooooooooooooooooooooooooooo";
  3. ?>

(don't forget to enclose any php script with <?php and ?> or it wont work - I cant put them in here otherwise it will run the script instead of showing the code. Also, change the onclick event in the html to call oranges.php (if you saved the php with that name) instead of oranges.txt

You can see that in action here

There may be a pause between you clicking and the message being displayed. We can let the user know that something is happening by adding another if condition to look for a readyState of 1 (loading) and displaying the message "off to get oranges..." like this..

HTML:
  1. if (XMLHttpRequestObject.readyState == 1)
  2. {
  3.     obj.innerHTML="off to get oranges...";
  4. }

we just chuck that into the anonymous function that is called when the readyState changes...

HTML:
  1. XMLHttpRequestObject.onreadystatechange = function()
  2.           {
  3.             if (XMLHttpRequestObject.readyState == 4 &&
  4.               XMLHttpRequestObject.status == 200) {
  5.         obj.innerHTML=XMLHttpRequestObject.responseText;
  6.  
  7.             }
  8.         if (XMLHttpRequestObject.readyState == 1)
  9.         {
  10.         obj.innerHTML="off to get oranges...";
  11.         }
  12.           }

See that in action here

You can download all the files used in this tutorial here.

Now we're getting somewhere! But, what if we want to send data to the php file so it can do something more than print a hardcoded message?
Actually, it's quite easy! come back next week to see the final part where we'll send something to the php file and have it send back something it made with what we sent. (it could be a username or contents of a text field - think form validation!)

You can subscribe to my feed with the button at the top right of this blog or add your email to the box on the sidebar of the main page to subscribe by email. I hope you can come back and learn a bit more, enough at least to make your own php script and AJAX page....

Popularity: 9% [?]

Category : Code | PHP | ajax

Introducing AJAX CommentLuv

Posted by Andy Bailey at 25 December, 2007, 4:38 pm
35

CommentLuv with AJAX

I have finally implemented AJAX into my CommentLuv plugin, it should dramatically increase the speed at which the comment gets posted after a user clicks "submit" by monitoring the comment form entry box, as soon as it gets some action then a javascript routine gets called to find the feed, scrape the last post from it and display it below the comment form.

Because the fetch_rss has a cache which it checks before it tries to open the remote feed, the last blog post will still be stored in the cache from the AJAX request so it wont need to do much at all to add the last blog post to the comment. sweet!

It's installed and running ok here in it's rough state so I can see how it performs while I neaten up the code so it can do all the includes and stuff automatically as well as detect another url change if the user changes their blog url after starting to comment.

At the moment it uses a bit of jquery and a bit of the AJAX goodness that you can see on the Beginners AJAX Tutorials posts, when I've figured out how to do it all with jquery and have it working nicely then I'll release the new updated plugin.

It seems that a quiet Christmas morning is all that's required to get an AJAX problem solved!

I'll get used to jQuery and get some tutorials out there after the part 5 and 6 are done with the current series. Happy Holidays everyone!

You can always find the current version of CommentLuv here
(no AJAX in the current version yet...)

Popularity: 7% [?]

Category : Blog News | Blog Tools | ajax

How to stop spam blogs from hotlinking your images with htaccess

Posted by Andy Bailey at 21 December, 2007, 1:11 pm
19

It's a pain in the arse when someone copies your content, especially when they hotlink your images so your bandwidth gets used instead of their own so here's a neat way of adding to your .htaccess file so you prevent particular blog networks from displaying images from your hosting by hotlinking them.
Use this code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myspace\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?blogspot\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?livejournal\.com/ [NC]
RewriteRule .*\.(jpe?g|jpg|gif|bmp|png)$ http://www.commentluv.com/images/antihotlink.jpg [L]
</IfModule>

Just open up your .htaccess file (found in the root of your web space) and add the above lines. That should prevent blogs from myspace,blogspot and livejournal from hotlinking your images. You can add the url of a blog you know is hotlinking your images by following the format of the other RewriteCond statements.

Change the image url for the RewriteRule to one you have hosted somewhere (not on the same hosting because that will get redirected in a loop every time it is linked to).

It works pretty well, look at this screen shot of a self confessed content copier....
Content Thief owned

There a couple of my posts on that blog and they are littered with images telling the (probably non existent) readers to come and read the article here instead. Another reason to always have an image in my articles!

It's a shame there isn't a way to do that with content though! If you are having trouble with spam blogs scraping your content and images, you can read a fantastic article here on what to do when someone steals your content by Lorelle who has a treasure trove of Wordpress and blogging information. Well worth a look!

Popularity: 5% [?]

Category : Blog Tools | Code

Beginners AJAX Tutorial Series Part 4

Posted by Andy Bailey at 20 December, 2007, 10:58 am
8

Ajax Avenue Street Sign

This is part 4 of the AJAX Tutorial Series.
You can see the other Beginners AJAX Tutorials here.

In this part of the tutorial series we will get ready to rumble! download data using the object we created in the last part...

onreadystatechange

What the hell is that? lol, it's not move states when you're ready.. it's what we use on the object we created to see what state it is in, the different states are:

  • 0 - Uninitialized
  • 1 - Loading
  • 2 - Loaded
  • 3 - Interactive
  • 4 - Complete

We can check for an objects state to see where it's at to decide if we can output the result or not, we can combine this with a test for the HTTP status of a download to see if it was successful, we can check for a value of 200 which means everything went ok. (a value of 404 would mean file not found just like normal HTTP status codes).

To use the readystate property, just bang it on the end of the object name and put that in an 'if' statement. Ie...

if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
{do something}

(the && means AND, so we check the readystate AND the status, if they are BOTH correct then execute the code in the curly brackets)

To put that in the code of the last tutorial, replace the bit that outputs the message about the oranges box being collected with an anonymous function that is attached to the objects readystate. It's not named an anonymous function because it has a secret identity, it is just a nice way of running a function with no name when a certain thing happens like the objects' readystate being changed.

For example, if we put
XMLHttpReqestObject.onreadystatechange = function()
{
do something
}

We have attached the onreadystatechange to the object we created, as soon as that objects readystate has altered, the anonymous function takes action and executes the commands contained in the curly brackets ({do something }).

That's exactly where we put the test to see what the HTTP status and readystate number is. Ie.

XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
{
data downloaded, do something with it...
}
}

You may have noticed little graphics that are synonymous with web 2.0 and AJAX ( ajax loading ). You can add one of those to let the user see something is happening. In the code above, you can check for a readyState of 1 (loading) and add some text like this:

XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 1)
{
obj.innerHtml="Loading...";
}

if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
{
data downloaded, do something with it...
}
}

The FiddyP amazing analogy:
Last time we had created a box for the oranges that we want from the AJAX shop
XMLHttpRequestObject = new XMLHttpRequest();
then we opened it and sent it to the AJAX shop with a label of what we want.
XMLHttpRequestObject.open("GET", dataSource);
And we wait for the signal that the box we sent was filled ok and ready to go
if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)

Your whole code will now look like this...

HTML:
  1.   <head>
  2.     <title>FiddyP AJAX Tutorial Series Part 4</title>
  3.     <script language = "javascript">
  4.       var XMLHttpRequestObject = false;
  5.       if (window.XMLHttpRequest) {
  6.         XMLHttpRequestObject = new XMLHttpRequest();
  7.       } else if (window.ActiveXObject) {
  8.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  9.       }
  10.       function getData(dataSource, divID)
  11.       {
  12.         if(XMLHttpRequestObject) {
  13.           var obj = document.getElementById(divID);
  14.           XMLHttpRequestObject.open("GET", dataSource);
  15.  
  16.           XMLHttpRequestObject.onreadystatechange = function()
  17.           {
  18.             if (XMLHttpRequestObject.readyState == 4 &&
  19.               XMLHttpRequestObject.status == 200) {
  20.             }
  21.           }
  22.        obj.innerHTML = "Ready to download";
  23.            
  24.         }
  25.       }
  26.     </script>
  27.   </head>
  28.     <H1>Fetching data with Ajax</H1>
  29.     <form>
  30.       <input type = "button" value = "Display Message"
  31.         onclick = "getData('oranges.txt', 'targetDiv')">
  32.     </form>
  33.     <div id="targetDiv">
  34.       <p>The fetched data will go here.</p>
  35.     </div>
  36.   </body>
  37. </html>

OK, so you have your box at the AJAX shop full of oranges.txt, next tutorial we'll do something with the oranges...

If you've been following the other tutorials then the code above shouldn't be so hard to understand (shyeah right!), it's just a bit more added to what we've already covered. You can copy the code from above and try it out yourself or you can click here to see it in action (hold on to your hat :-P)

We're still in the AJAX starting blocks waiting for the bang to start us on the AJAX runway but don't despair, next tutorial we will actually download data from a server and display it on the page without a reload which is what AJAX is all about. Once we can actually display the data, we can start doing things with it and then we can start to make pretty little scripts that do amazing things like count the amount of times a pink box has been clicked. hehehe, we'll do more than that I promise!

Popularity: 8% [?]

Category : Code | ajax

Beginners AJAX Tutorial Series Part 3

Posted by Andy Bailey at 11 December, 2007, 4:37 pm
18

Ajax Avenue Street Sign

This is part 3 of the AJAX Tutorial Series.
You can see the other Beginners AJAX Tutorials here.

In this part of the tutorial series we will open the object that you created in the last tutorial. So far, we have created an object to handle the data and made sure it is created in whatever browser you are using. Now, if we want to work with the created object, we need to open it...

Open The XMLHttpRequest Object

Opening the object prepares it for use to fetch the data from a server, the act of opening the object doesn't actually fetch any data yet.

The FiddyP amazing analogy :- Imagine you created a page to collect a box of oranges from the local AJAX fruit shop. In the last tutorial, you created the box (object), now you need to open it so the AJAX oranges can fit inside...

The open comand: [square brackets] = optional arguments.

open("method", "url" [, asyncFlag[, "username"[, "password"]]])

Explanation:

  • method is the HTTP method to use when opening the connection. It can be 'GET', 'POST', 'PUT' or 'HEAD'
  • url is the URL you want to download from
  • asyncFlag is a Boolean (one or the other) value used to indicate that the call is asynchronous. (no waiting for it to finish)
    This defaults to TRUE or 1 so is not needed unless you want to specify that the page waits for completion.
  • username is if you need a username to access the page identified by URL
  • password is for a password if it's required.

Make sense? lol, maybe not. Here's how it looks in use:

XMLHttpRequestObject.open("GET","oranges.txt");

We just add the open bit of code on to the end of the name of the object we created. Here's an example page where we create a form in HTML and add a button for the user to click to open a remote file on the server with AJAX. Because AJAX uses CSS to change elements of the page, we need to create a <div> on the page to show the data we retrieve from the remote file. We'll also have to create a function to take action when the button is clicked (we can't do it the normal way with the <form> action because that would cause the page to reload).

Here's the form and div html:

HTML:
  1.   <input type = "button" value = "Display Message" onclick = "getData('oranges.txt', 'targetDiv')">
  2. </form>
  3.  
  4. <div id="targetDiv">
  5.   <p>The oranges box will go here.</p>
  6. </div>

You can see that the button has an 'onclick' event. That's the javascript bit that calls the function when the button is clicked. The <div> has an id so the function will know where to put the collected data.

The javascript function is held in the <head> part of the document;

JAVASCRIPT:
  1. function getData(dataSource, divID)
  2.       {
  3.         if(XMLHttpRequestObject) {
  4.           var obj = document.getElementById(divID);
  5.           XMLHttpRequestObject.open("GET", dataSource);
  6.  
  7.           obj.innerHTML="Oranges Box Opened!";
  8.         }
  9.       }

The function is called with the name of the data file we want (dataSource) and where to put the output (divID)
The first line checks if the XMLHttpRequestObject was created (we did that last time so you know it should get to this point)
If it finds that the object was created, it moves on to set the variable obj to the <div> we created by using getElementByID
Next, we use the open command on the object we created earlier, set the option to GET and point it to the external data file we specified when calling the function.
Once that is done, we can output a message to the <div> we assigned to the variable obj by using .innerHTML=

We'll just put this function in place of the "hello world" message that we put in the last tutorial..

The whole code looks like this...

HTML:
  1.   <head>
  2.     <title>FiddyP AJAX tutorial, open the object</title>
  3.     <script language = "javascript">
  4.       var XMLHttpRequestObject = false;
  5.       if (window.XMLHttpRequest) {
  6.         XMLHttpRequestObject = new XMLHttpRequest();
  7.       } else if (window.ActiveXObject) {
  8.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  9.       }
  10.  
  11.       function getData(dataSource, divID)
  12.       {
  13.         if(XMLHttpRequestObject) {
  14.           var obj = document.getElementById(divID);
  15.           XMLHttpRequestObject.open("GET", dataSource);
  16.           obj.innerHTML="Oranges Box Opened!";
  17.         }
  18.       }
  19.     </script>
  20.   </head>
  21.  
  22.     <H1>Fetching data with Ajax</H1>
  23.     <form>
  24.       <input type = "button" value = "Display Message"
  25.         onclick = "getData('oranges.txt', 'targetDiv')">
  26.     </form>
  27.     <div id="targetDiv">
  28.       <p>The Oranges box will go here.</p>
  29.     </div>
  30.   </body>
  31. </html>

You can copy the contents of the HTML shown above and save it as a HTML file, you can also create a file called oranges.txt and put it in the same directory as the HTML file. (it doesn't matter what is in the text file as we wont be doing anything with it other than to open it). Open the HTML file in your browser and try it yourself or click here to see it in action if you can't wait.

That's it for this week, we're still not doing anything major yet but, be happy! we have so far, created an object and opened it. YEY! come back for part 4 where we will prepare to download information from the file we opened. (oh, I bet you can't wait!!)

beep beep w00t!

Popularity: 6% [?]

Category : Code | ajax

Beginners AJAX Tutorial Part 2

Posted by Andy Bailey at 3 December, 2007, 1:09 pm
12

Ajax Avenue Street Sign

This is part 2 of the AJAX Tutorial Series. You can see the Beginners AJAX Tutorial Part 1 here.

In this part, we will start you off with some very simple code just to show you how things get started. At the end of this tutorial you will have made your own 'hello world' AJAX script... (not a big deal I know, but you have to start somewhere!). I'll keep these tutorials bitesize and introduce a new command or concept on each tutorial to add to what has already been covered. Today, we start at the beginning with something that will be in all future scripts...

The XMLHttpRequest Object

The what? :-)
The XMLHttpRequest object is at the heart of AJAX. It allows you to send and receive data with javascript without refreshing the page and works on all browsers (except, you guessed it! Internet Explorer). All you have to do is find out if the browser supports it with an 'if' statement in the javascript contained in the <head> part of the html page and then create a XMLHttpRequest object using the 'new' command;

JAVASCRIPT:
  1. if (window.XMLHttpRequest) {
  2.         XMLHttpRequestObject = new XMLHttpRequest();}

That would create a XMLHttpRequest object and store it in XMLHttpRequestObject.

But, what about Internet Explorer?.. We can't use XMLHttpRequest so we have to use an ActiveXObject. For us to do anything with an XMLHttpRequest and make sure it works on both IE and Firefox, we can add this to the javascript so we now have..

JAVASCRIPT:
  1. if (window.XMLHttpRequest) {
  2.         XMLHttpRequestObject = new XMLHttpRequest();
  3.       } else if (window.ActiveXObject) {
  4.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  5.       }

OK, that should cover us for making a XMLHttpRequest object, we can't do anything in this tutorial series without it so to be sure, lets just create the whole html document and test to see if it can make an object by adding another 'if' statement to output some text to the screen if the object creation was successful using the javascript command 'document.write()'.

HTML:
  1.   <head>
  2.     <title>FiddyP Ajax Tutorial Part 2 - Test object creation</title>
  3.  
  4.     <script language = "javascript">
  5.       var XMLHttpRequestObject = false;
  6.  
  7.       if (window.XMLHttpRequest) {
  8.         XMLHttpRequestObject = new XMLHttpRequest();
  9.       } else if (window.ActiveXObject) {
  10.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  11.       }
  12.  
  13.       if (XMLHttpRequestObject) {
  14.         document.write("<h1>Hello World! I Created an Object!</h1>");
  15.       }
  16.     </script>
  17.   </head>
  18.  
  19.  
  20.   </body>
  21. </html>

You can click on the title of the code snippet above and see the plain text version, just copy this to your own file and save it as an HTML document or right click here and save it and test it. If it's working, it will display the message "hello world! I created an object!" in big letters.

YEY! we created a 'hello world' AJAX program!, I know it's not much use yet but this is the foundation for all future tutorials so even thought it's not that 'sexy' , it IS important to make sure we can create the object that will handle the data being sent to and received from a remote file.

We can get into the sexy ins and outs of using this on the next tutorial. Please come back again to see how we can use this to open a remote file and display it's contents on a HTML page without a reload...(ooh, I can't wait....)

:-)

Popularity: 5% [?]

Category : Code | ajax

Beginners AJAX. Tutorial series. Part 1

Posted by Andy Bailey at 20 November, 2007, 11:33 am
19

Ajax Avenue Street Sign

This is the first in a series of tutorials about AJAX and how to use it on your site. Each new tutorial will take the subject a little bit further and hopefully, it wont be long before you start creating your own AJAX applications for your own site and revel in the wonderness of geekdom :-)

It seems more and more people want to use this type of programming method to bling their blog up to 2.0 level including me!, so come along as I traverse the wonders of AJAX. I am learning this as I go so it's not really fair to call it a tutorial series but you get the idea!

First off, we need to know what AJAX is...

Part 1.
AJAX Introduction.

The first thing to know about AJAX is that in fact, it isn't actually a programming language. It's just a way of using other languages together to create an 'application' style of interface for a web page.

The word 'AJAX' comes from a contraction of 'Asynchronous', 'Javascript', 'And', 'XML', it's purpose is to allow a web page to send and receive small bits of data 'asynchronously' while the user is still on the page and change the contents of parts of the page with the data received.

It could be something simple like automatically updating a shipping charge after a customer enters their postal code in a form or it could be something more familiar like the Google auto-guess/complete that happens when you start typing in a search term.
Google labs auto guess
As soon as you start typing something in the search box, the javascript on the page detects each key press and sends it to the server which then sends back any terms that have the same letters starting the term, as you continue to type it shows a narrowing set of options and how many results they each have. You can try it out yourself at http://labs.google.com/suggest

Here are some other examples of sites using AJAX.

The important thing to remember is that AJAX is the term used when the page needs to send and/or receive data to the server asynchronously, whereas a sliding DIV or a dynamic menu is just Javascript or DHTML.


The difference between normal and AJAX web applications

A traditional web application(using a shopping cart form example)
Traditional web application diagram
.. a user enters their shipping details and clicks submit, the page sends the form details and reloads again showing the shipping charge.

AJAX web application
Ajax web application diagram
.. a user enters their postal code and the server automatically calculates the shipping charge and returns it to the original page, all without the user having to wait for another page load. Much more efficient!


How does it do it?

It's the work of the devil! that's how! :-)
Seriously though, javascript, css and html are used on the browser side and a scripting language is used on the server. The scripting language can be PHP, perl, asp or any kind of get-process-return language that runs on a server.

Really, javascript is the boss for all the actions as it is responsible for orchestrating everything on the client side. ie:
The javascript detects the click, text entry or other action which initiates a call to the script on the server which responds with the data requested, the javascript receives the data and by using css and html it is able to update the page, all without the user having to wait for a page load as with traditional web pages.

Take a look at this diagram:
AJAX process

  1. The javascript detects an event and requests information from the server
  2. The server accepts the request, processes it and returns it's own data
  3. The javascript accepts the data and uses CSS and HTML to put it on the page

Data types

Probably the best data type used is XML, simply because it is a standard capable of representing complex data that would otherwise be difficult to represent. But, that doesn't mean that XML is the only type of data that can be used. You could use plain text if there was only a small amount of simple data to be sent like a name or value.

You could also use something called JSON (JavaScript Object Notation) but that's for a later tutorial (and for geekier types!)


The Server role

It's important to remember that AJAX is entirely dependent on a server program accepting requests and responding to them in some way. You can't just make a web page with some Javascript on it and have an AJAX application, you need the javascript to send and receive information and do something with it.

You don't have to use the same server as the web page to do the processing, you could just as easily use a script on a remote server.
Places like Google, Flickr and others allow people to use an API (Application Programming Interface) to request information from one of their scripts on their server. Flickr for example could send back information listing a users photo locations so the AJAX page could display them.

Simply, whatever the server or javascript does, it all comes down to ; request send, request received, data processed, data sent back, data used.


Ok! that's the introduction dealt with, I hope that clears up exactly what AJAX is for you!

On the next part of this series, we will get into the guts of an AJAX application and show you just how it works and how you can change it yourself. For now, here is an example of what you should be able to do after a couple more tutorials..

It detects a click on the pink area and sends a request to a PHP script that reads a text file containing a number, adds one to it, saves it and sends back the number to be displayed on the page. Try it yourself, it may take a couple of seconds to show the number (this is just an example, later we will add a nice AJAX'y type of wait animation). I wonder how high this number will/can go? :-)

Practical Example

  • Click Me! clicks so far :

Come back next week for the second part of the series where we will get into the innards of AJAX and show you just whats needed to make the traditional 'hello world' program....

Popularity: 7% [?]

Category : Code | ajax
follow fiddyp on twitter

20 online now
the most online was 176
elottery