jQuery - AJAX - Zend Framework:

Create JSON response for jQuery

You can create new controller, or use any exist:
  1. <?php
  2. require_once 'Zend/Controller/Action.php';
  3.  
  4. /**
  5.  * Ajax Controller
  6.  */
  7. class AjaxController extends Zend_Controller_Action
  8. {
  9. /**
  10.   * index Action
  11.   * @return void
  12.   */
  13. public function indexAction()
  14. {
  15. // check is AJAX request or not
  16. if (!$this->getRequest() -> isXmlHttpRequest()) {
  17. $this->getResponse()-> setHttpResponseCode(404)
  18. -> sendHeaders();
  19. $this->renderScript('empty.phtml');
  20. return false;
  21. }
  22. // requery php library
  23. require_once 'jQuery.php';
  24. // assign to div with id = 'test' current time
  25. jQuery('div#test')->html(date('H:i:s'));
  26. }
  27. }
Template index.phtml - only jQuery response.
  1. <?php
  2. require_once 'jQuery.php';
  3. // only jQuery response, nothing more
  4. jQuery::getResponse();
  5. ?>
Include JavaScript in current action of controller (where your use AJAX)
  1. $this->view->headScript()->appendFile('/public/js/jquery.js');
  2. $this->view->headScript()->appendFile('/public/js/jquery.php.js');
Call AJAX in your current template
  1. <a href="#" onclick="javascript:$.php('<?php echo $this->Url(array('controller'=>'ajax', 'action'=>'index'))?>',{});return false;">click me</a>