2011-06-21 20 views
0

我是symfony框架的新手。我試圖根據在states選擇框中完成的選擇填充city_id表單字段。如何在symfony 1.4(form-field population)中使用ajax

我創建命名的getStates actions.class.php文件()

這裏是它的代碼的新方法。

public function executeGetCities(sfWebRequest $request) 
{ 
    $this->forwardUnless($query = $request->getParameter('stateId'), 'users', 'index'); 

    $this->states = Doctrine_Query::create() 
        ->from('States s') 
        ->where('s.id = ?', array($request->getParameter('stateId'))); 

    if($request->isXmlHttpRequest()) 
    { 
     echo json_encode($this->states); 
    } 
} 

上狀態的變化事件的JavaScript代碼如下:

$('#users_states').change(function(){ 
      populateSelectBox('users_city_id', 'get', '<?php echo url_for('states/getCities/'); ?>', {stateId:$(this).val()}) 
     }); 

功能populateSelectBox只需通過JSON迭代並填充它在city_id表單字段。

但上面的請求給出了錯誤,並說視圖getcities不存在。

請幫我這麼做。

回答

1

您是否有特殊的路線來處理您的路由中的這種情況?
如果使用默認路由,則必須首先創建一個特殊路由,並使用stateId的特定參數。 您可以在瀏覽器中手動測試生成的網址(您必須在操作中禁用ajax測試)。

而你的教義請求未被執行!你可以用fetchArray返回數組:

public function executeGetCities(sfWebRequest $request) 
{ 
    //$this->forwardUnless($query = $request->getParameter('stateId'), 'users', 'index'); 

    $this->states = Doctrine_Query::create() 
        ->from('States s') 
        ->where('s.id = ?', array($request->getParameter('stateId')))->fetchArray(); 

    //if($request->isXmlHttpRequest()) 
    //{ 
     echo json_encode($this->states); 
    //} 
} 
+0

哇......那工作的人......改變了執行()來fetchArray(),結果就很好..謝謝... :) –