2

I created a list in view by iterating items in an session array over a loop. I even put check boxes for each iteration. I need to get the checked values as a string or an array of strings in to controller. How can I do that. This is the view part.

 <?php
                $session = Yii::$app->session;
                $array = explode('\r\n', $session['cdr_val']) ;                                  
             ?>
                <div class="box-header with-border">
                    <h3 class="box-title">CDR Configurations</h3>
                    <?= Html::a('<span class="glyphicon pull-right glyphicon-transfer"></span>', ['#'], ['data-url' => Url::toRoute(['cdr/compare']), 'id' => 'btn-compare', 'title' => 'Compare']); ?>
                </div>

                <div class="box-body">
                    <!--form class="form-horizontal" name="form_blacklist_table" id=""-->
                    <div class="row">
                        <div class="box-body boxpad contSeperator">
                            <div class="col-md-12 col-sm-12 col-xs-12">
                                <table id="analyse-table" class="table table-bordered table-striped table-hover">
                                    <thead>
                                        <tr>
                                            <th class="actions-fix-wd-1 text-center">Select</th>
                                            <th>CDR</th>
                                            <th class="actions-fix-wd-2 text-center">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                         <?php if ($array[0]!=''){ ?>
                                        <?php $j = 0 ?>
                                        <?php do{ ?>
                                        <tr>
                                            <td class="text-center">
                                           <!--?=
                                           //$form->field($model, 'status')->checkbox(['value' => "$j",'encode'=>false,'label'=>null]) ?-->

                                           <input name="checkbox1" id="checkbox1" type="checkbox" value="<?= $j ?>">
                                        </td>
                                            <td>
                                                <div>
                                                    <?php
                                                        echo($array["$j"]);
                                                        ?>
                                                </div>
                                            </td>

                                            <td class="text-center actions">
                                                <?= Html::a('<span class="glyphicon glyphicon-check"></span>', ['#'], ['data-url' => Url::toRoute(['cdr/analyze','id'=>$array["$j"]]), 'id' => 'btn-view', 'title' => 'analyze']); ?>
                                            </td>
                                            <?php $j++ ?>
                                        </tr>
                                        <?php }while($j<sizeof($array)); ?>
                                        <?php } ?> 

And i used javascript part to trigger the checkboxes, And it successsfully triggered the checkbox checked values. but i couldn't any way to send it to controller.

$urlView1 = Url::to('cdr/compare');
$script1 = <<< JS
$( document ).ready(function() {
    $(document).on('click', '#btn-compare', function(e) {

        var items=document.getElementsByName('checkbox1');
        var selectedItems="";
        for(var i=0;i<items.length;i++){
            if(items[i].type=='checkbox' && items[i].checked==true){
                selectedItems+=","+items[i].value;
            }
        }
        //console.log("{$urlView1}")

        /*$.ajax({
            url : "http://localhost:8080/index.php?r=cdr/compare",
            type : 'post',
            data: 'items=' + selectedItems,
            success : function(data){
                console.log(data);

                $('#view-comp').attr("src", $(this).attr('data-url'));
                $('#ViewModal').modal({show:true})
                var data = JSON.parse(data);
            },
        });*/
        $('#view-comp').attr("src", $(this).attr('data-url'));
        $('#ViewComp').modal({show:true})
        return false;            
});
});
JS;
$this->registerJs($script1);

here is the necessary controller part

public function actionCompare(){
$model = new CdrAllinone();
        $session = Yii::$app->session;
        $this->layout = 'popup';
        print_r(Yii::$app->request->getUrl());
print_r($_POST);

And model has the variables defined and returned.

3
  • And i'd really like to know if there is a way to transfer selectedItems variable in js part to controller. at least how to attach it to url Commented Oct 12, 2019 at 15:50
  • It is done by using get method in request. Commented Oct 12, 2019 at 18:56
  • It does not get the value. maybe when a popup called, data in the variable gets cleared idk. always gives NULL Commented Oct 12, 2019 at 19:35

1 Answer 1

1

Try this:

        ...
        <?php if (!empty($array[0])): ?>
        <?php foreach($array as $key => $value): ?>
            <tr>
                    <td class="text-center">
                    <!--?=
                    //$form->field($model, 'status[]')->checkbox(['value' => "$key",'encode'=>false,'label'=>null]) ?-->

                    <input name="checkbox1[]" id="checkbox1" type="checkbox" value="<?= $key ?>">
                </td>
                    <td>
                <div>
                        <?= $value ?>
                </div>
                    </td>

                <td class="text-center actions">
                    <?= Html::a('<span class="glyphicon glyphicon-check"></span>', ['#'], ['data-url' => Url::toRoute(['cdr/analyze','id'=> $value]), 'id' => 'btn-view', 'title' => 'analyze']); ?>
                </td>          
            </tr>
        <?php endforeach; ?>
    <?php endif; ?>
        ...

Hope it Helps

Sign up to request clarification or add additional context in comments.

10 Comments

still didn't work. the submit button i used for checkbox is at top outside the loop. Plus the js variable selecteditems got the value i want. i don't know how to transfer it to controller.post method always print out as null
@Sandeepa Kariyawasam post this in your controller return \Yii::$app->request->post("items"), and comment everything else.
the value has printed as NULL.despite whatever checkbox i clicked. console shows the selected value. Is there anyway i can attach the value to url and get it from controller?
@Sandeepa Kariyawasam Try this data : { items : selectedItems },
Still the output gives NULL.console log shows the checked data correctly so issue won't be there.
|

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.