Ajax Request

Hi!

Hope you are doing well.

I need your help in implementing ajax request… Couldn’t able to find any useful resources…

I have simple Html::dropdownList() in view

so onchange event of dropdownlist i need to query all the records based on id using ActiveDatProvider in controller and return response to same page and should display as GridView…

How to implement it i am trying from couple of days could able get around the solution…

I am not getting any response from controller…

Codes

In View




<div class="row chit-group ">

    <?= Html::tag('br'); ?>

    <?= Html::tag('br'); ?>

    <?= Html::tag('h3', 'Select Chit Group') ?>

    <?= Html::dropDownList('chit_grps', null, $listData, ['prompt'=>'Select Chit Group', 

        'id'=>'chit_grp',

        'onchange'=>'ajaxRequest(this.value)'

        

        ]); ?>

</div>

<?php

echo Html::script("function ajaxRequest(value){ 

       $.ajax({

        type: 'post',

        url:chitgrp, // if i use url here it show error Uncaught ReferenceError: chitgrp is not defined

        data:{'chit_grp':'value'},

        success: console.log(value)

        });

        

        }

        

       ");




if(isset($dataProvider1)){

?>

<?= GridView::widget([

        'dataProvider' => $dataProvider1,

        

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

            'chit_grp_name',

            'no_of_subscribers',

            'chit_duration',

            'chit_tot_amt',

            'chit_status',

            ['class' => 

                'yii\grid\ActionColumn',

                'template' => '{view}',

                'urlCreator' => 

                                function ($action, $model, $key, $index) 

                                {

                                    if ($action === 'view') 

                                    {    

                                        $url ='chitview?id='.$model->chit_grp_id;

                                        return $url;

                                    }

                                }

            ],

            ['class' => 

                'yii\grid\ActionColumn',

                'template' => '{update}',

                'urlCreator' => 

                                function ($action, $model, $key, $index) 

                                {

                                    if ($action === 'update') 

                                    {    

                                        $url ='chitupdate?id='.$model->chit_grp_id;

                                        return $url;

                                    }

                                }

            ],

        ],

        

    ]); 

    ?>




<?php

}

?>

In Controller




public function actionChitgrp()

        {

            $dataProvider = new ActiveDataProvider([

            'query' => ChitGroup::find()->where(['organization_id'=>Yii::$app->session->get('organization_id')]) 

            ]);

            if(Yii::$app->request->isAjax)

            {

                $id = Yii::$app->request->post()['chit_grp'] ;

                $dataProvider1 = new ActiveDataProvider([

                'query' => Bidding::find()->where(['id'=>$id])->all() 

                ]);

                return $this->renderAjax('chitgrp', ['dataProvider1'=>$dataProvider1]);

            }else

            {

                return $this->render('chitgrp', ['dataProvider'=>$dataProvider]);

            }

        }

It will really great full if have any reference links to how to use specifically ajax in yii2…

Thank you

Your success handler should be a function, not just Javascript code. Try


success: function() { console.log('whatever'); }

Also you need to really need to use logging so that you know exactly at what point it is failing. You say the controller doesn’t reply but is that because it is not called? Because it errors? Because it succeeds but the ajax callback fails?

You are not likely to get answers to such complex questions because it will take people a long time to check all your code, if you can get the question to a much more specific example or problem, it is much easier for people to answer.

Thank you Lukos… i figured it… i was not processing ajax success response… ::) ::)