PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Related Books
The Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP

The Essential Guide to Dreamweaver CS3 with CSS, Ajax, and PHP

With over 3 million users worldwide, Adobe's Dreamweaver is the most popular web development...

The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP (The Essential Guide)

The Essential Guide to Dreamweaver CS4 with CSS, Ajax, and PHP (The Essential Guide)

Dreamweaver CS4 is a massive step forward in terms of integration with the rest of the CS4 suite...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Cloning Google Suggest With Ajaxac

Creating The Getsuggestions Action Handler Callback

Now that we have the ability to lookup suggestions from the database, we need to create an action in our application, and a handler to take care of calls to the action. We will deal with actually initiating the action call later.

In AjaxAC, we define an action as a thing that can happen from a subrequest. That is, when you initiate a HTTP subrequest, you perform an action on the application.

We need only one action this application: getsuggestions.

First we need to tell GoogleSuggestCloneJax that the action exists, then we tell it how to handle it.

To tell it the action exists, we use the following code in our constructor (it’ll be included in context further down this page):

Listing 6 listing-6.php
<?php
    $this->registerActions('getsuggestions');
?>

It is called ‘registerActions’ and not ‘registerAction’ because it is possible to register more than one action (each as a separate argument).

Now to create the handler callback. All action handlers use the following naming structure: action_[actionname]. So in this case, the callback is called action_getsuggestions. Action handlers don’t take any parameters.

With the callback, the class now looks like this:

Listing 7 listing-7.php
<?php
    require_once('AjaxACApplication.class.php');
 
    class GoogleSuggestCloneJax extends AjaxACApplication
    {
        // ... other code ... //
 
        function GoogleSuggestCloneJax()
        {
            parent::AjaxACApplication();
            $this->registerActions('getsuggestions');
        }
 
        function action_getsuggestions()
        {
            $suggestions = array();
            $this->getSuggestions($this->getRequestValue('q'), $suggestions);
            $this->sendResponseData('jsarray', $suggestions);
        }
 
        // ... other code ... //
    }
?>

One thing to note here is that the term to fetch suggestions for is in the request parameter ‘q’. In AjaxAC, request parameters are what appear in the ‘get’ string. So realistically, this corresponds to $_GET[‘q’].

Finally with this function, we use the AjaxAC handler for sending subrequest data back. There are several return types available, but we’re just going to send data back as a multidimensional array (each array element will have 2 items: item 0 is the suggestion term, and item 1 is the number of results that term would yield in a search).

In This Article


Tagged in , , ,