php - Yii2. Sending data by ajax in GridVIew isn't works

254

I want to change status of order in line in GridView, by pushing buttons (check, cross) enter image description here

But when I see my $_POST in Controller it has only csrf token, when I make a mistake?

My GridView:

<?= GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
        'attribute' => 'car_id',
        'value' => 'car.state_num'
    ],
    [
      'attribute' => 'driver_id',
      'value' => 'driver.name'
    ],
    'status',
    'first_date',
    'second_date'

    ['class' => 'yii\grid\ActionColumn',
    'template' => "{confirm} {decline}",
      'buttons' => [
        'confirm' => function ($url, $model, $key='new_stat') {
          return Html::a('<span ></span>', $url, [
            'title' => Yii::t('yii', 'confirm'),
            'data-confirm' => Yii::t('yii', 'Сдать авто?'),
            'data-method' => 'post',
            'data-pjax' => 1,
          ]);
        },
        'decline' => function ($url, $model, $key='new_stat') {
          return Html::a('<span ></span>', $url, [
            'title' => Yii::t('yii', 'decline'),
            'data-confirm' => Yii::t('yii', 'Отказ?'),
            'data-method' => 'post',
            'data-pjax' => 2  ,
          ]);
        }
      ],
      'urlCreator' => function ($action, $model, $key, $index) {
        if ($action === 'confirm') {
          $url ='contracts';
          return $url;
        }
        if ($action === 'decline') {
          $url ='contracts';
          return $url;
        }
      }
    ]
  ]
]); ?>

And my Controller:

public function actionContracts()
{

    if(Yii::$app->request->post())
    {
        print_r($_POST);
        die();
    }

}

Why it does not send data? When i push the button it has only:

Array ( [_csrf] => P-VomWJo5fwNwRi8uGnoMrw-uJPvLK9TM-k4k_Ka5SVQvF_VDR6Cj3unc_6BD6B-2Xf-27ln1RJHik32uf6RQA== )

884

Answer

Solution:

What data are you trying to send? And how are you trying to send it, I cannot see that in the code sample you sent.

You can add parameters to the request adding them to thedata-params attribute of your link.

For example:

'template' => "{confirm} {decline}",
'buttons' => [
 'confirm' => function ($url, $model, $key='new_stat') {
  return Html::a(
   '<span ></span>', 
   $url, [
    'title' => Yii::t('yii', 'confirm'),
    'data-confirm' => Yii::t('yii', 'Сдать авто?'),
    'data-method' => 'post',
    'data-pjax' => 0,
    'data-params' => [
     'param_name_1' => 'param_value_1',
     'param_name_2' => 'param_value_2',
     'param_name_3' => 'param_value_3',
   ],
 ]);
 },
 'decline' => function ($url, $model, $key='new_stat') {
  return Html::a(
   '<span ></span>', 
   $url, [
    'title' => Yii::t('yii', 'decline'),
    'data-confirm' => Yii::t('yii', 'Отказ?'),
    'data-method' => 'post',
    'data-pjax' => 0  ,
    'params' => [
     'param_name_1' => 'param_value_1',
     'param_name_2' => 'param_value_2',
     'param_name_3' => 'param_value_3',
   ],
  ]);
 }
],

Those parameters will be sent asPOST parameters.

People are also looking for solutions to the problem: php - Downsides to using __DIR__

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.