将JS变量传递给控制器​​并在Yii中查看

时间:2014-02-19 02:29:19

标签: javascript php yii parameter-passing

我是Yii的新手。我有一个javascript变量。必须将此ID传递给控制器​​和相应的视图。我在访问控制器中的JS变量时遇到问题。请帮我。如果传递了JS变量,我将如何检查传递的变量。提前谢谢,

我从JS变量(id)传递的视图:

<div style="float:left;padding-left:20px;">
                    <?php echo $form->labelEx($model,'host_customer_id'); ?>
                    <?php 
                    echo $form->textField($model,'host_customer_id',array('style'=>'width:420px;'));
                    $this->widget('ext.select2.ESelect2', array(
                        'selector' => '#NimsoftHost_host_customer_id',
                        'options' => array(
                            'allowClear'=>false,
                            'placeholder' => 'Search Other Customers',
                            'minimumInputLength' => 3,
                            'quietMillis'=>100,
                            'ajax' => array(
                                'url' => Yii::app()->createUrl('Nimsoft/customers'),
                                'dataType' => 'jsonp',
                                'data' => 'js: function(term,page) {
                                        return {
                                            q: term, 
                                            //ctype: $("#itsmIncidents_cloudcustomer input:radio:checked").val(),
                                            page_limit: 10,
                                        };
                                    }',
                                'results' => 'js: function(data,page){
                                    return {results: data.details};
                                }',
                            ),

                             'formatResult'  => 'js:function(data){
                                return data.name;
                              }',
                            'formatSelection' => 'js: function(data) {
                                return data.name;
                            }',

                        ),
                    ));
                    ?>
                    <div style="float:right;padding-left:10px;">
                        <?php 
                        echo CHtml::link('view details', array('/Nimsoft/ciLink'), array(
                        'onclick'=>'return hs.htmlExpand(this, { objectType: "iframe", wrapperClassName: "full-size",height:500, align: "center" } )',
                            'class'=>'btn btn-block btn-success',
                            'style'=>'width:100px;display:none;',
                            'id'=>'customer_details_pop',
                         ));
                        ?>
                   </div>
                 </div>


<?php $this->endWidget();?>

<script>
$("#NimsoftHost_host_customer_id").on("change", function(e) { 
         <?php echo CHtml::ajax(array(
            'url'=>array('Nimsoft/loadCustType'),
            'data'=> "js:{'NimsoftHost[host_customer_id]':e.val}",
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {

                // enable customer link
                $('#customer_details_pop').css('display','block');
                $('#customer_details_pop').attr('href', 'Nimsoft/details/'+data.customerid);
} ",
        ))?>;
     })
</script>

这里我传递data.customerid。我想知道这个值是否属于控制器。如果它去了,如何在我的视图中打印(details.php)。

我的控制器操作:

public function actionDetails()
        {
          $this->render('details');
        }

1 个答案:

答案 0 :(得分:1)

您的customerId变量应作为查询字符串传递。

例如:

链接中的网址应为:

 $('#customer_details_pop').attr('href', 'Nimsoft/details/id/'+data.customerid);

在您的控制器中,您可以使用...

检索ID
public function actionDetails($id)
{
   $this->render('details',array('customerId'=>$id));
}

有关详情,请参阅Yii documentation on Url Management

上的以下内容