Chapter 8. AJAX / Dynamic and Interactive Forms

AJAX is a buzzword that hit the Web in 2005, thanks to Jesse James Garrett, a user-experience expert who founded AdaptivePath.com. (Oddly enough, he’s not a JavaScript or XML expert; you can ask him yourself!). If you’re totally new to AJAX, I’ll just point out that, at its core, AJAX is nothing that scary or horrendous. AJAX isn’t even a new technology or language!

AJAX is an acronym for Asynchronous JavaScript And XML, and it is a technique for using JavaScript and XML to send and receive data between a web browser and a web server. Its biggest advantage is that you can dynamically update a piece of a web page or web form with data from the server (preferably formatted in XML) without forcing the entire page to reload. This technique has made possible for many web developers to make advanced web applications, sometimes called RIAs (Rich Interface Applications), that work and feel more like software applications than web pages.

However, AJAX is starting to have another meaning. For example, a Microsoft web developer may use VBScript instead of JavaScript to serve Access database data that is transformed into an XML stream using a .NET server-side script. Such a site would still be considered “Ajaxy”.

In fact, it’s getting to the point where just about anything on a website (other than Flash) that slides, moves, fades, or pops up without rendering a new browser window is considered to be “Ajaxy”. In truth, a large portion of these sites don’t truly qualify as using AJAX; they’re just using neat JavaScripts. Generally, if you use cool JavaScripts in your Joomla! template, your site will probably be considered “Ajaxy”, despite not being asynchronous and not using any XML.

We’re going to look at the most popular methods so that you can get going with AJAX in Joomla! and create interactive and dynamic forms for your Joomla! site. We will also look at some cool JavaScripts and JavaScript toolkits that you can use to appear “Ajaxy”.

Note

Know nothing of this “AJAX” business? The W3Schools site has an excellent introduction to AJAX explaining it in straight-forward simple terms. They even have a couple of great tutorials that are fun and easy to accomplish even if you only have a little HTML, JavaScript, and server-side script (PHP or ASP) experience (no XML experience is required). You can find it at http://w3schools.com/ajax/.

Preparing for Dynamic and Interactive Forms

Gone are the days of clicking, submitting, and waiting for the next page to load. A web page using AJAX techniques (if applied properly) will give the user a smoother and leaner experience. If you click on a drop-down option and the checkbox menus underneath are updated immediately with the relevant choices: no submitting, no waiting. Complicated forms that, in the past, took two or three screens to process can be reduced to one convenient screen by implementing the form with AJAX.

As wonderful as this all may sound, I must again offer a quick disclaimer. I understand that, as with drop-down menus and Flash, many of your clients are demanding AJAX for their sites, but AJAX techniques are best used in situations where they truly benefit the user’s experience of a page: for example, if they Cut a lengthy web-process form from three pages down to one. In a nutshell, using an AJAX technique simply to say that your site is an AJAX site is probably not a good idea.

You should be aware that, if not implemented properly, some uses of AJAX can compromise the security of your site. You may inadvertently end up disabling key web browser features (like back buttons or the history manager). Then there are all the basic usability and accessibility problems that JavaScript in general can bring to a site.

Some screen readers may not be able to read a new screen area that’s been generated by JavaScript. If you cater to users who rely on tabbing through content, navigation may be compromised when new content is updated. There are also interface design problems that AJAX brings to the table (and Flash developers can commiserate). Many times, in trying to limit screen real estate and simplifying a process, developers often end up creating a form or interface that is complex and confusing, especially when the user is expecting the web page to act like a normal web page!

You Still Want AJAX on Your Site?

OK! You are here and reading this chapter because you want AJAX in your Joomla! template. I’ll only ask you to take the above into consideration and do one or more of the following to prepare.

Help your client to assess their site’s target users first. If everyone is Web 2.0 aware, using new browsers, and fully able to use a mouse, then you’ll have no problems, atleast not with AJAX. But if any of your users are inexperienced with RIA sites or have accessibility requirements, you will need to take some extra care. Again, it’s not that you can’t or shouldn’t use AJAX techniques; just be sure to make allowances for these users. You can easily adjust your site’s user expectations upfront by explaining how to expect the interface to act. Again, you can also offer alternative solutions and templates for people with disabilities or browsers that can’t accommodate AJAX techniques.

Note

Remember to check with Don’t Make Me Think, the Steve Krug book that we recommended in Chapter 7 for help with any interface usability problems you may run into. Also, if you’re really interested in taking on some AJAX programming yourself, I highly recommend AJAX and PHP by Cristian Darie, Bogdan Brinzarea, Filip Chereches-Tosa, and Mihai Bucica. In this book you’ll learn the ins and outs of AJAX development, including handling security issues. You’ll also do some very cool stuff such as making your own Google-style auto-suggest form and a drag-and-drop sortable list (and that’s just two of the many fun things to learn from this book).

You’re now equally warned and armed with the knowledgeable resources about AJAX that I can think to throw at you. Let’s see how to get something “Ajaxy” into your Joomla! site.

Joomla! Extensions

As we’ve discovered several times in this book, a huge advantage of working with an open-source tool such as Joomla! is that it has a great network of developers associated with it and anyone can contribute. If you’ve ever thought you needed or wanted something for your Joomla! website, it is likely that someone else has had the same thought and has created a suitable extension that works with Joomla!.

Note

Choosing an extension: Joomla! extensions are a great resource, and the most reliable place to start is at http://extensions.joomla.org. There are hundreds of thousands that do incredibly useful things and require no heavy programming experience on your part. When researching extensions, especially those that use techniques that may compromise security, be sure to read all the information about the extension before installing it, including any user reviews provided. As with most extensions offered on the Joomla.org site, you should be able to go to the developer’s site and probably contact them directly with any questions that you may have about the extension. Be aware that not all extensions are free or Open-Source like Joomla! itself. It will be up to you to decide if an extension is worth paying for.

The type of form that you’ll most likely want to use on your site is a comment form. Comment forms are a great way to build a community around your site, not to mention beefing out your content base with user feedback. (This is something to think about if you’ll be supplementing the site with Google or AdBrite advertisements. The text that users leave via your comment forms will increase the keywords that will serve up advertisements via these two services creating an opportunity to make more PPC/PPI cash.) Using AJAX techniques within a comment form enables people to post without waiting for the entire page to reload again.

Joomla! Comment 2.40 is an extension that you can add to various pieces of Joomla! content (static pages or regular content pages). It doesn’t rate quite as well as some other comment extensions that use AJAX techniques, but at the moment, it’s the highest ranking one, and it is free using the GNU GPL license.

You can download it from here:

http://extensions.joomla.org/component/option,com_mtree/task,viewlink/

link_id,677/Itemid,35/

(Forgive the long URL; you can also just go to http://extensiona.joomla.org and search for the extensions by name.)

Installing Joomla! Extensions

Working with Joomla! Comment 2.40 is pretty straightforward. You download the ZIP file and go to Installers | Components. From there, you can browse for the ZIP file, and select Upload File and Install just as for a Module extension.

Note

Is it a module or a component? You’ll have to pay attention to the extension’s name and instructions to find out if it is a module or a component. (For instance, the Extend Menu extension we worked with in Chapter 7 was a module, not a component). You’ll know which installation screen to use. What’s the difference? Generally, modules can appear on any page and are usually kept pretty simple. Components, on the other hand, usually load as their own page (or area of a page, the way the Banners component works) and can be more complex.

When you get the Upload component – Success screen, you can go to the Components | Joomla! Comments | Edit Settings where you’ll be able to see the General, Layout, and Security options available for the component.

Installing Joomla! Extensions

Figure 8.1 Installing a new component

You’ll notice that the component will appear on every page unless you specify that certain pages should exclude it.

Installing Joomla! Extensions

Figure 8.2 Joomla! Comment 2.4 options and features

Note

Using More Than One AJAX Extension Most AJAX Joomla! extensions make use of the XAJAX toolkit. There is a problem running multiple extensions that reference the toolkit—only one Joomla! extension will work. If you’re going to install multiple extensions that use this XAJAX toolkit, you’ll need to download the solution from http://www.xajax-joomla.com/.

Writing Your Own Extensions If you already have a little AJAX or PHP experience and you feel that it could be reused as an extension in other sites, I recommend that you check out Building Websites with Joomla! by Hagen Graf. Chapter 11 will walk you through programming and building your own Joomla! extensions.

Wrapping It Up

What if you’re an AJAX/PHP pro and find it easy to build a simple form for a site, but do not thinks that it is worth taking the time to make your own extension? Perhaps the site you’re upgrading to Joomla! already had a working AJAX form? Why mess around looking up other people’s extensions hoping they come close to duplicating what you need?

Never fear, you don’t need to reinvent the wheel by writing your own extension or worry about how you are going to get complicated XHTML markup that references JavaScripts and server-side scripts into a Joomla! content page. Enter the wrapper.

The Wrapper Menu Item creates a content page that wraps an external web page in an iFrame. The iFrame content appears as a “page within a page”. Hence, if your content is an external website (such as the joomla.org wrapper sample in the “out of the box” installation of Joomla!), then all the site’s functions, menus and so on, will be operable from within the wrapper.

There’s really no good reason to load someone else’s website into your own. However, displaying content that uses non-Joomla! scripts (like your own or existing AJAX scripts), galleries or directories, within your Joomla! template has just got a whole lot easier. You can also use the Wrapper Menu Item to load a page that houses a Flash SWF. (If you don’t want to paste the object embed and JavaScript check tags we discussed in Chapter 7, into a content page.) You can also display pages written in specialized web languages that most Joomla! text editors can’t accommodate. The wrapper is indeed a wonderful thing.

Note

Remember that we discussed disabling aspects of your browser. The browser’s back button only affects the page loaded directly into it and not anything loaded into the iFrame. If a user is working with an AJAX form you’ve loaded into a page via the Wrapper Menu Item, you’ll need to give clear instructions on how to handle the form. For instance, if the user feels they’ve made a mistake and hits the back button to try to go back and correct it, they’ll be taken to the previous content page they (which will be an entirely different page), and not the form’s previous state. Make sure that the user understands this, and give them clear instructions about how to navigate through the form’s states.

Using the Wrapper Menu Item

Take a look at this tutorial from W3Schools.com. It’s a great AJAX script that can be used to keep track of a simple CD collection: http://w3schools.com/php/php_ajax_xml.asp.

Note

Watch out! The script above and many scripts that support AJAX nowadays require your server to be running one of the newest versions of PHP (we’re using PHP 5.12). If the script doesn’t work for you, it gives you an Unexpected T_OBJECT_OPERATOR error, and upgrading your version of PHP may be in order.

You may find that a client already has this kind of script in place before choosing to upgrade a site to the Joomla! CMS. Is so, you’ll be able to get the AJAX scripts looking and working great with just a minimal amount of work to the front-end XHTML. (Just make sure that you remove the old site’s header, nav, and footer includes from the page.) Remember, this is an independent page loading into an iFrame! You’ll need to add a style sheet to it, since it will not pick up your Joomla! template’s CSS file automatically.

You’ll be able to easily integrate many perfectly good existing AJAX applications into their new Joomla! page using the Wrapper Menu Item.

  1. Take the script above, which is a basic XML-driven AJAX application that lets us list CDs and Artists (or any existing AJAX form you have access to), and place it on your server.

  2. In your Joomla! Administration Panel, go to Menu | mainmenu (or any other menu you want the wrapper to be accessible from), and then click New.

    Using the Wrapper Menu Item

    Figure 8.3 Adding a new menu item

  3. From the New Menu Item screen, select Wrapper from under the Miscellaneous section.

  4. Name the link and include the URL path to the AJAX script.

    Using the Wrapper Menu Item

    Figure 8.4 New menu item wrapper options

  5. In the right panel you can set several different parameters for the wrapper component: height, width, scrollbars, and more.

  6. Hit Save and your new item should be in the menu you specified.

Note

Don’t want the wrapper content visible from one of your menus? Because this component is a menu item, this is the easiest way to get a wrapper page started. Once you have made the page, click on its link off the menu and copy and save it. You can then unpublish the menu item, but still reference the page from anywhere by referencing the link that you saved.

In Figure 8.5, you can see our XML-driven CD lister loaded into Joomla! via the Wrapper module.

Using the Wrapper Menu Item

Figure 8.5 AJAX, auto update CD view form

The AJAX Factor

As we mentioned, it’s easy to give your site an “Ajaxy” feel regardless of asynchronously updating it with server-side XML, just by sprucing up your interface with some snappy JavaScripts. The easiest way to get many of these effects is to reference a JavaScript toolkit or framework. The two leading favorites in the AJAX community are:

Prototype is more of a base framework and Script.alico.us is more of a set of libraries for neat effects. In fact, Script.alico.us references the Prototype framework, so your best bet is probably to use Script.alico.us, but be sure to check out Prototype’s site and try to understand what the framework does.

By using toolkits or frameworks such as those above, you will be able to implement their features and effects with simple calls into your Joomla! pages. (Again you will need to turn off the WYSIWYG editor to add custom HTML code that references these toolkits.)

Review the above and choose the one that suits your needs best. Then download and reference it within your Joomla! template’s index.php header tags:

<script src="javascripts/prototype.js" type="text/javascript">
                                                            </script>
<script src="javascripts/scriptaculous.js" type="text/javascript">
                                                            </script>

JavaScript Components and Libraries

The fun doesn’t stop there! There are many other JavaScript-effect components and libraries that are built using the two above. One of the most popular scripts out there that make a big hit on any website is Lightbox JS, which can be found at http://www.huddletogether.com/projects/lightbox2/.

This is an extremely easy-to-implement script. After downloading it, add the key scripts to your Joomla! template’s index.php header file:

<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects">
                                                            </script>
<script type="text/javascript" src="js/lightbox.js"></script>

Be sure to add the required CSS sheet to the index.php file as well!

<link rel="stylesheet" href="css/lightbox.css" type="text/css"
                                                    media="screen" />

Then, you can create a page in the Joomla! CMS, turn off the WYSIWYG editor for your user, and add in basic href links around your image tags as follows:

<a href="images/image-1.jpg" rel="lightbox" title="my caption">
                            <img src="images/image-1-thumb.jpg"/></a>

That’s it! You can also add the page as a wrapper, as we mentioned earlier, but if you leave the greyed-out background turned on in the JavaScript and CSS file, the greyed out area will give away your wrapper’s iFrame borders.

JavaScript Components and Libraries
JavaScript Components and Libraries

Figure 8.6 Shots of JS Lightbox on http://Fransozo.com/home. Site built by http://mediaetc.com

More Joomla! Extensions!

Again, if you have a feature that you think you’d like to use across multiple Joomla! projects, someone else has probably thought of the need too. Don’t forget to review the extension’s library at Joomla.org to see what’s available before cooking up something from scratch.

Here are a couple of good GNU GPL license extensions that can be used to give your site some instant “Ajaxy” slickness (and hopefully “stickyness”)

Both of these items are components, so you’ll install them as we installed the Joomla! Comment 2.4 component earlier. Again, both of these extensions aren’t true AJAX, but the slick factor is bound to impress your users.

Summary

In this chapter, we reviewed the most common ways to get Ajaxy with your site, and we downloaded and installed two extensions and looked at how to use the wrapper in detail to house an existing, fully-working AJAX application. Up next: some final design tips for working with Joomla!.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.209