【问题标题】:Pass JS variable to controller and view in Yii将 JS 变量传递给控制器​​并在 Yii 中查看
【发布时间】:2014-02-19 02:29:18
【问题描述】:

我是 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');
        }

【问题讨论】:

  • url 中的 data.customerid 将是查询字符串而不是会话变量。你知道 urlManager 吗?
  • 我已经更改了我的代码,请现在说

标签: javascript php yii parameter-passing


【解决方案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

【讨论】:

  • 它给了我一个错误:错误 400 您的请求无效。
  • 如果我写 echo $id; 就够了吗?在视图中?
  • protected/config/main.php 中的 urlManager 配置项是否有以下条目? '//'=>'/',
  • 是的。
  • 谢谢,我得到了ID,它给了我45c48cce2e2d7fbdea1afc51c7c6ad26,这个值和客户没有任何关系,怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多