Archive for the ‘ajax’ Category

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: 5% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

OMGG! (oh my golly gosh) I have had the most tremendously hard week. I’ve had 2 different web sites to produce, an hour long power point presentation for a shop display. Make a barcode program that can create a barcode from a product name, build 2 systems that can talk to each other but still be protected against outside intrusion and finally, design and print a variety of business cards for clients.

I couldn’t have done half of the coding and designing if it wasn’t for the wonderfulness of the internet!

Some sites that were instrumental to me learning new things were:

ErracticWisdom where I (finally) learned how to create a layout from scratch using css (no tables!). I used to make websites with photoshop, slice them up and save for web which would make some ugly table based pages that for lack of any other way of creating the site, had to do. The problem with table based layouts is that they are ugly, it all takes a long time to load and they don’t always behave nicely when you want to add extra content.

That problem is now solved, I took what I learned from Tom Fadial and experimented like crazy and now I am super stoked that I can create some pretty snazzy looking websites that load in a split second and are xhtml compliant. yey! thanks Tom!

Trevor Davis.net had an excellent tutorial on how to make an AJAX contact form which was absorbed, modified and implemented into my new .css based site and it turned out to be shockingly good. Enough to get me another project! Nice one Trevor!

There’s also jquery which I have been using for some experimentation lately, it’s a fantastic AJAX library which takes an awful lot of the pain out of coding AJAX pages. It’s so fantastic that you can create really simple effects and AJAX loadings in seconds.

I’ve even managed to sell a few iMaingo speakers direct from this site via Google Checkout!

Everything else has been put on hold while all these projects get completed so apologies to those wanting to see part 6 of the AJAX tutorial series, it’ll be here soon and it’ll be worth the wait!

uVme has now launched, it’s been a wait but hopefully all the kinks have been worked out and we’ll all be ready to make some money with it soon (although the site was performing poorly today). Come on VWD! get your act together, you’ve had over a year to get this right and I still can’t edit my profile. pah!

Popularity: 8% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

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: 10% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

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% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

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% [?]

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]