0

I am new to Yii. I have a javascript variable. This ID must be passed to the controller and to the respective view also. I have a problem in accessing the JS variable in the controller. Please help me. If the JS variable is passed, how will I check the passed variable.? Thanks in advance,

My view from where JS variable(id) is passed:

<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>

here I am passing data.customerid. I want to know this value goes to controller or not. If it goes, how to print that in my view(details.php).

My controller action:

public function actionDetails()
        {
          $this->render('details');
        }
2
  • The data.customerid in the url will be a querystring not a session variable. Do you know about the urlManager? Commented Feb 19, 2014 at 2:41
  • I have changed my code, please say now Commented Feb 19, 2014 at 2:43

1 Answer 1

1

Your customerId variable should be passed as a query string.

For example:

The url in your link should be:

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

and in your controller you can retrieve the id with ...

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

For more info please read the following on Yii documentation on Url Management

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

6 Comments

It gives me an error: Error 400 Your request is invalid.
is it enough if I write echo $id; in view?
Does your urlManager config item in protected/config/main.php have the following entry? '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
Ya its having the same.
Thanks, I got the ID, it gives me 45c48cce2e2d7fbdea1afc51c7c6ad26, and this value is no where related to the customer, what to do?
|

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.