Demo: AJAX Autocompleter with Script.aculo.us and a Zend JSON web service

by jon on March 24, 2008

Today I'm posting a sample project of a modification of a Script.aculo.us auto-completer script that will work with a Zend JSON web service. This example could easily be expanded by providing a richer data collection to the client (currently it's just a string array).

Here's a demo:

Implementing the code is really only one line once you have the back end web service configured:

 var demoAutocompleter = new MyAjaxAutocompleter('tbAutoCompleter', 'dvAutoCompleter', '/demo/html/Ajaxservice/index', {});

where 'tbAutoCompleter' corresponds to the textbox you wish to bind, 'dvAutoCompleter' is the control in question and '/demo/html/Ajaxservice/index' is the actual Web service giving you JSON data.

I'm still having a hard time POSTing to a Zend controller in a manner as elegant as you would find in ASP.NET's web services, however the code isn't really that bad. My AjaxServiceController looks like this:

class AjaxserviceController extends Zend_Controller_Action
{
	public function init()
	{
		$this->_helper->contextSwitch()
             ->addActionContext('index', 'json')
             ->initContext();
 
	}
 
	public function indexAction()
	{
 
       	$this->_helper->viewRenderer->setNoRender();
       	$this->_helper->getHelper('layout')->disableLayout();
 
       /* [Json response] */
		 $data = array("2008","3d","social","software","technology","tips",
"tools","toread","travel","tutorial",
"tutorials","tv","typography", "wiki","windows","wishlist","wordpress"
,"work","writing","youtube");
 
		 $responseData = $this->array_isearch($this->getRequest()->getParam('search') , $data);
 
       try {
 
           $responseDataJsonEncoded = Zend_Json::encode($responseData);
           $this->getResponse()->setHeader('Content-Type', 'application/json')
							   ->setBody($responseDataJsonEncoded);
 
       } catch(Zend_Json_Exception $e) {
           // handle and generate HTTP error code response, see below
       }
 
	}
 
    function array_isearch($str, $array) {
    	$returnArray = array();
        foreach($array as $v) {
            if(preg_match('/$str/i',$v))
            	$returnArray[] = $v;
 
        }
        return $returnArray;
    }
}
 

There's not much here, mostly just disabling the Zend Layout, specifying the content and doing the search. The $data array could easily be replaced with a data source (like a collection from Zend_Db).



Download Ajax Auto Completer Sample Zend Framework 1.5 project

The nice thing about web service AJAX controls is that they live independently of the application calling them, so you can essentially separate out your data layer in the same way that you would separate Zend_Db resources from a Zend_Controller.

{ 4 comments… read them below or add one }

David Dressler February 25, 2009 at 5:17 pm

It appears the demo isn’t working anymore. http://lebensold.net/demo/html/Ajaxservice/index returns 404 not found.

Thanks for all the Zend framework tutorials they have been very helpful.

Jon Lebensold February 25, 2009 at 5:40 pm

As David mentioned, the demo no longer works (I’ll have to thank Dreamhost for changing my Apache configuration). You can still download the source and it should work like any other Script.aculo.us file.

Also, I’ve posted some AJAX examples on Zendcasts using jquery:
http://www.zendcasts.com/making-json-objects-from-zend_controllers-using-jquery/2009/02/

Willem Luijk March 2, 2009 at 4:17 pm

Hi Jon,

This tut is a nice start for getting customer numbers. It would be nice to update a label next to the input with customers name when choosing a value …

Christine Fürst June 19, 2009 at 11:22 am

Hi Jon,

Thanks for giving me a starting point for auto completion within the zend framework. It definitely helped me.

Cheers, Stinie

Leave a Comment

Previous post:

Next post: