2017-05-09 15 views
0

Yii2でkartik select2プラグインを使用することにより、国と州の依存関係をドロップダウンしようとします。Yii2 Yii2でのAjaxリクエスト

国の

フォームフィールドと述べている:

$url = yii\helpers\Url::toRoute('op-client/lists'); 

$this->registerJs($this->render('script.js'), \yii\web\VIEW::POS_READY); 


$form->field($model, 'country_id')->widget(Select2::classname(), [ 
            'data' => $countryData, 
            'language' => 'en', 
            'options' => ['placeholder' => 'Select'], 
            'pluginOptions' => [ 
             'allowClear' => true, 
            ], 
            'pluginEvents' => 
            [ 
             'change' => 'function() 
              { 
               getstate 
               (
               $("#select2-opclient-country_id-container").val(), 
               "'.$url.'" 
              ) 
              }', 
            ], 
           ]).' 


           $form->field($model, 'states_id')->widget(Select2::classname(), [ 
            'data' => $statesData, 
            'language' => 'en', 
            'options' => ['placeholder' => 'Select'], 
            'pluginOptions' => [ 
             'allowClear' => true, 
            ], 

           ]).' 

Script.js

function getstate($countryid,url) 
{ 
    //console.log(startdate + enddate); 
var csrfToken = $('meta[name="csrf-token"]').attr("content"); 

    $.ajax({ 
     type:"POST", 
     cache:false, 
     url:url, 
     data:{countryid:countryid, _crsf:csrfToken}, 
     success:function(data){ 
      $("#select2-opclient-states_id-container").val(data); 
     }, 
    }) 
} 

コントローラ:私はプログラムを実行すると

public function actionLists() 
    { 
     $request = Yii::$app->request; 

     $country = $request->post('countryid'); 

     $countStates = OpStates::find() 
         ->where(['country_id' => $country]) 
         ->count(); 

     $states = OpStates::find() 
         ->where(['country_id' =>$country]) 
         ->all(); 

     if($countStates > 0) 
     { 
      foreach($states as $state){ 
       echo "<option value='".$state->id."'>".$state->state_name."</option>"; 
      } 
     } 
     else 
     { 
      echo "<option></option>"; 
     } 
    } 

、それは誤り「キャッチされないにReferenceErrorを表示:countryidは定義されていません "。 しかし、私はすでにそれにカントリードを渡したと思った?私はどこで間違っていますか?

助けてください/アドバイスをいただければ幸いです。ありがとう

+0

'... getstate($ countryid、..'と '... countryid:countryid、..'関数の引数で** $ **! – leninhasda

答えて

1

以下のコードを確認してください。あなたはcountry_id変数名に少し間違いがあったと思います。

public function actionLists() 
    { 
     $request = Yii::$app->request; 

     $country = $request->post('country_id'); 

     $countStates = OpStates::find() 
         ->where(['country_id' => $country]) 
         ->count(); 

     $states = OpStates::find() 
         ->where(['country_id' =>$country]) 
         ->all(); 

     if($countStates > 0) 
     { 
      foreach($states as $state){ 
       echo "<option value='".$state->id."'>".$state->state_name."</option>"; 
      } 
     } 
     else 
     { 
      echo "<option></option>"; 
     } 
    } 

、ここ

function getstate(countryid,url) 
{ 
    //console.log(startdate + enddate); 
var csrfToken = $('meta[name="csrf-token"]').attr("content"); 

    $.ajax({ 
     type:"POST", 
     cache:false, 
     url:url, 
     data:{countryid:countryid, _crsf:csrfToken}, 
     success:function(data){ 
      $("#select2-opclient-states_id-container").val(data); 
     }, 
    }) 
} 

それはあなたの問題を解決します。

+0

ああ私も間違いを見つけました。ありがとう – ron