2012-11-01 128 views
1

我控制器Zend框架中使用AJAX

public function init() 
{ 
    /* Initialize action controller here */ 
    $ajaxContext = $this->_helper->getHelper('AjaxContext'); 
    $ajaxContext->addActionContext('get-ajax-content', 'html') 
       ->initContext(); 
} 

public function indexAction() 
{ 
    // action body 
    $form = new Zend_Form_Element_Select('menu'); 
    $parent_array=range('1','9'); 
     $form->addMultiOptions(array($parent_array)); 
     $this->view->form = $form; 
} 

這個表單元素,我打電話的AJAX功能,

<script type="text/javascript"> 

    $(document).ready(function(){ 
    $("#menu").change(function(){ 
     $.ajax({ 
      type: "POST", 
      url: "get-data", 
      data: "val="+$(this).val(), 
      success: function(result){ 
       $("#div_sortorder").html(result); 
      }, 
      error: function(){ 
       //console.log('error'); 
      }, 
      complete: function(){ 
       //console.log('complete'); 
      } 
     }); 
    }); 
</script> 

和獲取數據的行動是繼

public function getDataAction() 
{ 
    // action body 
    $this->view->message = 'This is an ajax message!'; 
    $params = array( 'host'  =>'localhost', 
         'username' =>'root', 
         'password' =>'', 
         'dbname' =>'cms' 
         ); 
    $DB = new Zend_Db_Adapter_Pdo_Mysql($params); 
    $section_id=$this->getRequest()->getPost('val'); 
    $menu_order='SELECT * FROM `cms_content_mst` WHERE section_id='.$section_id; 
    $menu_order=$DB->fetchAll($menu_order); 
    $newContent='<select id="sortorder" name="sortorder">'; 
    if(!empty($section_id) || $section_id != 0){ 
     if (empty($menu_order)) { 
        $newContent.='<option value="1" selected="selected">1</option>'; 
     } 
     else 
     { 

     for ($i=1; $i <= count($menu_order) ; $i++) { 

        $newContent.='<option value="'.$i.'"'; 
        if($i==count($menu_order)) 
        { $newContent.=' selected'; } 
        $newContent.='>'.$i.'</option>'; 

      } 

     } 
     } 
     $newContent.='</select>'; 

    $this->view->new = $newContent; 

} 

我已經完全工作jQuery。我有.get函數,但在調用.ajax它不起作用 我是newbee到zend plz,幫助

+0

[我給了一個很好的答案:對於如何使用jQuery/Ajax和ZF] [1] 解釋是初學者。 [1]:http://stackoverflow.com/questions/8485726/using-jquery-to-post-data-in-zend-framework/8489447#8489447 – tasmaniski

回答

0

請!檢查控制檯是否可以加載頁面。

$.ajax({ 
      type: "POST", 
      url: "get-data", 
      data: "val="+$(this).val(), 
      success: function(result){ 
       $("#div_sortorder").html(result); 
      } 

我想你給了錯誤的網址。