3

I need to get checked checkbox value from my view to controller when button is press.

Gridview :

<div class="installment-ready">
<!-- <h1><?= Html::encode($this->title) ?></h1> -->
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
    'columns' => [
    [
    'class' => 'yii\grid\SerialColumn',
    'contentOptions'=>[ 'style'=>'width: 60px'],
    ],
//Checkbox
[
    'class' => 'yii\grid\CheckboxColumn',
    'contentOptions'=>[ 'style'=>'width: 50px'],
    'name' => 'checked',
    'checkboxOptions'=> function($model, $key, $index, $column) {
     return ["value" => $model->ACCOUNT_ID];
    }
],

Button to process selected checkbox:

<?= Html::a('Submit', ['#'], ['class' => 'btn btn-success pull-right']) ?>
5
  • update question with info of Yii::$app->request->post(). Commented Nov 10, 2017 at 10:24
  • I already try to post the data, but it not show in controller. This is one of the checkbox '<input type="checkbox" name="checked[]" value="45678">'. I think the checkbox itself not contain my value Commented Nov 10, 2017 at 10:25
  • hum, have you try in your controller -> var_dump(Yii::$app->request->post()); ? The button submit a form? Commented Nov 10, 2017 at 12:47
  • when I var_dump, the data not post to my controller. array(0) { } Commented Nov 11, 2017 at 2:22
  • To test it you use jquery. when click on the submit button you can test if your checkbox are checked and relative value. I don't know your code, but i think you doesn't pass your value via POST method to the controller Commented Nov 15, 2017 at 8:25

2 Answers 2

1

In your controller action use below code

Yii::$app->request->post('checked');
Sign up to request clarification or add additional context in comments.

1 Comment

I already try to post the data, but it not show in controller. This is one of the checkbox <input type="checkbox" name="checked[]" value="45678">. I think the checkbox itself not contain my value.
0

I found a solution. I'm using ajax to post selected checkbox to my controller.

Script that I use in my view

<script type="text/javascript">
  // action for all selected rows
  function submit(){
    var dialog = confirm("Are you sure to submit the installment?");
    if (dialog == true) {
        var keys = $('#grid').yiiGridView('getSelectedRows');
        // console.log(keys);
        var ajax = new XMLHttpRequest();
        $.ajax({
            type: "POST",
            url: 'index.php?r=installment/submit', // Your controller action
            data: {keylist: keys},
            success: function(result){
              console.log(result);
            }
          });
    }
  }
</script>
<!-- Submit button -->
<button type="button" onclick="submit()" class="btn btn-success pull-right">Submit</button

And you can use usual Yii::$app->request->post() in controller to get your data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.