Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Yii2中的Yii2 Ajax请求_Javascript_Php_Jquery_Ajax_Yii2 - Fatal编程技术网

Javascript Yii2中的Yii2 Ajax请求

Javascript Yii2中的Yii2 Ajax请求,javascript,php,jquery,ajax,yii2,Javascript,Php,Jquery,Ajax,Yii2,通过在Yii2中使用kartik select2插件,我尝试对国家和州进行依赖下拉 国家/地区和州的表单字段: $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::cl

通过在Yii2中使用kartik select2插件,我尝试对国家和州进行依赖下拉

国家/地区和州的表单字段:

$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>";
        }
    } 
public function actionlist()
{
$request=Yii::$app->request;
$country=$request->post('countryid');
$countStates=OpStates::find()
->其中(['country\u id'=>$country])
->计数();
$states=OpStates::find()
->其中(['country\u id'=>$country])
->全部();
如果($countStates>0)
{
foreach($states作为$state){
回显“$state->state_name.”;
}
}
其他的
{
回声“;
}
} 
当我运行程序时,它显示错误“未捕获引用错误:未定义countryid”。 但我以为我已经把国家身份证传进去了?我哪里做错了


如有任何帮助/建议,将不胜感激。谢谢

请检查下面的代码,我认为您在country\u 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>";
        }
    } 

它将解决您的问题。

请检查下面的代码,我认为您在country\u 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>";
        }
    } 

它将解决您的问题。

在函数参数中,getstate($countryid,…
…countryid:countryid,…
您有一个$!
在函数参数中,getstate($countryid,…
…countryid:countryid,…
您有一个$!我也发现了我的错误。谢谢我也发现了我的错误。谢谢