Search code examples
zend-frameworkzend-formzend-dbzend-form-element

jquery ajax in Zend framework


i am new to ZF i want to create ajax link that will go to "task" controller and "ajax" action do something like this

$registry = Zend_Registry::getInstance();  
$DB = $registry['DB'];
$sql = "SELECT * FROM task ORDER BY task_name ASC";
$result = $DB->fetchAll($sql);

than put the result in this div

<div id="container">container</div>

this is my view where i am doing this

<?php echo $this->jQuery()->enable(); ?>
<?php echo $this->jQuery()->uiEnable(); ?>
<div id="container">container</div>
<?php  
echo $this->ajaxLink("Bring All Task","task/ajax",array('update' => '#container'));
?>

i dont know the syntax how i will do this , retouch my code if i am wrong i searched alot but all in vain plz explain me thanking you all in anticipation also refer me some nice links of zendx_jquery tutorial


Solution

  • This should work:

    class IndexController extends Zend_Controller_Action 
    {
        /**
         * Homepage - display result of ajaxRequest
         */
        public function indexAction()
        {
        }
    
        /**
         * Print result of database query 
         */
        public function ajaxAction()
        {   
            // disable rendering of view and layout
            $this->_helper->layout()->disableLayout();
    
    
            $registry = Zend_Registry::getInstance();  
            $db =  $registry['DB'];
    
            // get select object to build query 
            $select = $db->select();
            $select->from('task')->order('task_name ASC');
    
            // echo result or what ever..   
            $this->view->tasks = $db->fetchAll($select);
        }
    }    
    
    
    
    // index.phtml (view)
    <?php 
    
        echo $this->jQuery()->enable(); 
        echo $this->jQuery()->uiEnable();
    
        // create link to ajaxAction
        $url = $this->url(array(
            'controller' => 'index',
            'action'     => 'ajax',
        ));
    ?>
    
    <div id="container">container</div>
    
    <?php 
        echo $this->ajaxLink(
            "Bring All Task", $url, array('update' => '#container')
        ); 
    ?>
    

    and in your ajax.phtml

    <?php  if ($this->tasks): ?>
      <table>
        <tr>
             <th>task ID</th>
             <th>task Name</th>
        </tr>
      <?php foreach($this->tasks as $task) : ?>
        <tr>
           <td><?php echo $task['task_id']; /* depending on your column names */ ?>
           </td>
           <td><?php echo $this->escape($task['task_name']); /* to replace " with &quot; and so on */ ?>
           </td>
        </tr>
      <?php endforeach; ?>
      </table>
    <?php else: ?>
      No tasks in table.
    <?php endif; ?>
    

    regarding db you have to setup it first somewhere earlier in your code, for example front controller index.php or bootstrap.php, for example:

    $db = Zend_Db::factory('Pdo_Mysql', array(
      'host'     => '127.0.0.1',
      'username' => 'webuser',
      'password' => 'xxxxxxxx',
      'dbname'   => 'test'
    ));
    Zend_Registry::set('DB', $db);