weixin_33670786 2017-05-09 03:15 采纳率: 0%
浏览 218

Yii2中的Yii2 Ajax请求

By using kartik select2 plugins in Yii2, I try to make dependence dropdown for country and states.

Form field for country and states:

$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);
        },
    })
}

Controller:

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>";
        }
    } 

When I run the program, it show error "Uncaught ReferenceError: countryid is not defined". But I thought i passed the countryid into it already? Where have I done wrong?

Any help/advice will be appreciated. Thankss

  • 写回答

1条回答 默认 最新

  • weixin_33674976 2017-05-09 09:53
    关注

    Please check below code,i think you did little mistake in country_id variable name.

    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>";
            }
        } 
    

    and here

    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);
            },
        })
    }
    

    It will solve your issue.

    评论

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)