Zend Framework integration

For advanced users

Library

Copy jQuery.php and jQuery folder to your "library" directory.

Bootstrap

Add new action helper path:
require_once 'Zend/Controller/Action/HelperBroker.php';
Zend_Controller_Action_HelperBroker::addPrefix('ZendY_JQuery_Controller_Action_Helper');
Add path to jQuery view helpers:
// add new View helpers Path
require_once 'Zend/Layout.php';
 
$layout = Zend_Layout::startMvc($config->layout);
$layout->getView()->addHelperPath(APPLICATION_PATH . '/../library/ZendY/JQuery/View/Helper',
                                 'ZendY_JQuery_View_Helper_');

Controller

You can create new controller, or use any exist:
  1. /**
  2. * ajaxAction
  3. *
  4. * @access public
  5. * @created Tue Jun 10 17:50:24 EEST 2008
  6. */
  7. public function ajaxAction()
  8. {
  9. // init Jquery action helper - requried
  10. $this->_helper->getHelper('Jquery');
  11. // check XML HTTP Request if needed
  12. if ($this->_helper->getHelper('Jquery')->error('error', 'error')) {
  13. // forward to Error Controller, Error Action
  14. return false;
  15. }
  16.  
  17. // assign to div with id = 'test' current time
  18. jQuery('div#test')->html(date('H:i:s'));
  19.  
  20. // output response - requried
  21. $this->_helper->getHelper('Jquery')->sendResponse();
  22. }

View

Include JavaScript in current action of controller ("view" where your use AJAX):
<!-- Path to jQuery library -->
<?php echo $this->headScript()->prependFile('/js/jquery.js?ver=1.2.6'); ?>
<!-- Path to jQuery-PHP library -->
<?php echo $this->headScript()->appendFile('/js/jquery.php.pack.js?ver=0.6'); ?>
 
<div id="test"></div>
Call AJAX in your current template:
<a href="#" <?php echo $this->Ajax(array('action'=>'ajax'), array('id'=>23), true)?> >Test Ajax</a>
It generate next code:
<a href="#" onclick='javascript:$.php("/index/ajax",{"id":23});return false;' >Test Ajax</a>

Zend Framework integration

Simple way

Library

Copy jQuery.php and jQuery folder to your "library" directory.

Controller

You can create new controller, or use any exist:
  1. /**
  2. * ajaxAction
  3. *
  4. * @access public
  5. * @created Tue Jun 10 17:50:24 EEST 2008
  6. */
  7. public function ajaxAction()
  8. {
  9.  
  10. // check is AJAX request or not
  11. if (!$this->getRequest() -> isXmlHttpRequest()) {
  12. $this->getResponse()-> setHttpResponseCode(404)
  13. -> sendHeaders();
  14. $this->renderScript('empty.phtml');
  15. return false;
  16. }
  17. // assign to div with id = 'test' current time
  18. jQuery('div#test')->html(date('H:i:s'));
  19.  
  20. // output response
  21. jQuery()->getResponse();
  22. }

View

Include JavaScript in current action of controller ("view" where your use AJAX):
<!-- Path to jQuery library -->
<?php echo $this->headScript()->prependFile('/js/jquery.js?ver=1.2.6'); ?>
<!-- Path to jQuery-PHP library -->
<?php echo $this->headScript()->appendFile('/js/jquery.php.pack.js?ver=0.6'); ?>
 
<div id="test"></div>
Call AJAX in your current template
<a href="#" onclick="javascript:$.php('<?php echo $this->Url(array('action'=>'ajax')?>',<?php Zend_Json::encode(array("id"=>23))?>);return false;">click me</a>