2011-06-21 14 views
0

symfonyフレームワークを初めて使用しています。 states選択ボックスで行われた選択に基づいて、city_idフォームフィールドに値を入力しようとしています。symfony 1.4(フォームフィールドの母集団)でajaxを使用する方法

私はGETSTATESという名前actions.class.phpファイルに新しいメソッドを作成した()

ここにそれのためのコードです。 JSONを介し

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

機能populateSelectBox単に反復とcity_idフォームフィールドに移入:状態変化イベントに

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コードは以下の通りです。

しかし、上記のリクエストはエラーを示し、表示getcitiesは存在しません。

これを手伝ってください。

答えて

1

ルーティングでこのactioを処理する特別なルートがありますか?
デフォルトルーティングを使用する場合は、最初にstateIdの特定のパラメータを使用して特別なルートを作成する必要があります。 ブラウザで生成されたURLを手動でテストすることができます(アクションでajaxテストを無効にする必要があります)。

あなたのdoctrineリクエストは実行されません! (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を持つ配列を返すことができます –

関連する問題