How to send data with CHtml::ajax

Hi all,

I have a trouble when to send data from view to controller with Chtml::ajax

My View:




 <div class="row">

        <?php

            echo CHtml::activeTextField($tr,'name',array('id'=>'txtname');

             echo Chtml::button('New',array(

                    'onclick'=>"hello();"

                        ));

            

        ?>

    </div>

<script language="javascript" type="text/javascript">

        function hello()

        {

            var name = $('#txtname').val();

            <?php

                echo Chtml::ajax(array(

                        'url'=>array('user/new'),

                        'type'=>'get',

                        'data'=>"",

                        'success'=>"function(string){

                            $('#show').html(string);

                            }"

                ));

            ?>

        }

    </script>



My controller




 public function actionNew()

        {

            var_dump($_GET);

            

        }



But i don’t know how to put valuable on “data” in ajax options.

Thank

‘data’=>name, -> is this not working




 <div class="row">

        <?php

            echo CHtml::textField('txtname','');

             echo Chtml::button('New',array(

                    'onclick'=>"hello();"

                        ));

            

        ?>

    </div>

<script language="javascript" type="text/javascript">

        function hello()

        {

            var name = $('#txtname').val();

            <?php

                echo Chtml::ajax(array(

                        'url'=>array('corporation/asd'),

                        'type'=>'get',

                        'data'=>'js:$("#txtname").val()',

                        'success'=>"function(string){

                            alert(string);

                            }"

                ));

            ?>

        }

    </script>



this???

not working

I would advise to keep custom jquery separate from yii


'data'=>'js:$("#txtname").val()',

works, I tried it, maybe you can use serialize()

thank, I’ll try

Hi,

‘data’ could be a valid js call prefixed with ‘js:’ (this call should return a collection or a query string), or a query string, or even an array.

When you do not set ‘data’ and set ‘type’ property the CHtml class will set ‘data’ for you with serialized values of your form.

But it is not the case in your example. You have no form.

Remember, that your ‘success’ callback you also should prefix with ‘js:’ (you don’t have to with CHtml::ajax, but it’s a good practice, because later it is encoded with CJavaScript::encode method).

The whole would look like this:




            <?php

                echo Chtml::ajax(array(

                        'url'=>array('corporation/asd'),

                        'type'=>'get',

                        'data'=>'js:{name:$("#txtname").val()}',

                        'success'=>"js:function(string){

                                        alert(string);

                                    }"

                ));

            ?>



this is going to send an ajax request to /corporation/asd.html?name=test (if you use path for your urlFormat)

I wanted to use a drop down box with ajax for each row in CGridView and this thread saved my life.

This is the code for anyone needs it.




 array(

                'type' => 'raw',

                'header' => 'Status',

                'name'=>'status',

                'value' => 'CHtml::dropDownList("status-".$data->detailID,$data->status,Yii::app()->params["deliverystatus"],  array("ajax"=>array("type"=>"POST", "data" => "js:{status:jQuery(this).val()}", "url" => "ordersdelivery/update?id=".$data->detailID)))',

                'htmlOptions'=>array('style'=>'text-align:center;min-width:30px;')

            ),