Unable to post the data from view to controller in Yii2

21
May 18, 2019, at 3:10 PM

I am working on Yii2. I have a gridview with checkbox and on a button click I am redirecting it to an action controller using ajax.

 <?= Html::a('Disconnect', ['dco'], ['class' => 'btn btn-success', 'id'=>'dco']) ?>
<?php Pjax::begin(); ?>    
        <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [

        ['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($d) {
            return ['value' => $d['msn']];
        }],

        'ref_no',
        'dept_code:ntext',
        'dept_name:ntext',
        'allowed_units',
        'msn',
        'units_consumed',
        [
            'label' => 'Disconnected',
            'attribute' => 'disconnected',
            'format'=>'raw',
            'contentOptions' => ['style'=>'text-align:center'],
            'value' => function($model){
                return $model->disconnected == 1 ? '<span class="glyphicon glyphicon-ok text-success"></span>' : '<span class="glyphicon glyphicon-remove text-danger"></span>';
            },
            'filter' => Html::activeDropDownList($searchModel, 'disconnected', [''=>'All','1'=>'Yes','0'=>'No'], ['class' => 'form-control']),
        ],
        'diconnected_at',
        'reconnected_at',
        'active_energy_total_m',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>
<?php Pjax::end(); ?>

JS

$(document).ready(function () {  

 //DCO 
 $('#dco').on('click',function(e) {

       e.preventDefault();    
 var strValue = "";        
    $('input[name="selection[]"]:checked').each(function() {
    if(strValue!=="")
        {
        strValue = strValue + " , " + this.value;
        }
    else 
       strValue = this.value;     
});       
 $.ajax({
    url: '$DCOurl',
    type: 'POST',
    dataType: 'json',
    data: {data:strValue},         
    success: function(data) {
       alert(data);
    }
 });

 });
});

But when I click on the disconnect button it doesn't redirect to my controller. In console it gives me Not Found (#404): Page not found.

Update 1

Controller

 public function actionDco()
{
    if(Yii::$app->request->isAjax && Yii::$app->request->post())
    {
        $data = explode(',',$_POST['data']);
        var_dump($data);
        die();
    }
    else{
        $this->redirect('index');
    }


}

After updating the code as suggested I am able to go into my controller but still not able to get the data

I must be doing something wrong which I am not understanding

Any help would be highly appreciated.

Answer 1

You should assign a proper value to the post url (in your code you have assigned just a string '$DCOurl' not the content of the php var $DCOurl ) try

var myUrl = <?= "'". $DCOurl ."';" ?>
$.ajax({
  url: myUrl,
  type: 'POST',
  dataType: 'json',
  data: {data:strValue},         
  success: function(data) {
   alert(data);
  }
});
READ ALSO
add values of 5 text boxes and show the esult in 6th tetbo on the keypress vent of 5 th textbox

add values of 5 text boxes and show the esult in 6th tetbo on the keypress vent of 5 th textbox

i have 6 text boxes with id a,b,c,d,e,fout of these i enter value for 5

19
react-apollo Server Side Rendering

react-apollo Server Side Rendering

Disclaimer: I saw many other questions on SO which sound the sameBut no, this question and its context, everything is different

43
How detect window closed (new tab) in Vue?

How detect window closed (new tab) in Vue?

Need watch if that window open or closed

16
How to animate circular progress bar on page scroll down

How to animate circular progress bar on page scroll down

I have a circular progress bar that animates when the page loads, but I want it to animate when the user scrolls down to it, as it will be in the middle of the pageRight now if the page loads, the user does not see the animation

13