[Solved] Cgridview Custom Button Post Method Submission Not Working

Hi,

I am using custom update and delete buttons in CGridView. I have used the CButtonColumn to generate the buttons. In the url of the buttons http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail I have passed the id of the row and in the click event I have used jquery to submit the button using jquery POST method. The button is getting submitted, but if I check the $_POST variable in the controller the id is not available there. If I check in Mozilla browser’s network debugging tool, the value is getting submitted as POST method, but the id is still passed as query string along with the url. What could be wrong with this approach. Please help.

Thanks in advance.

post your source here

I am using CSqlDataProvider for CGridView.

In the url of button I am using

‘url’=>’$this->grid->controller->createUrl("/Extras/update", array(“id”=>$data[“id”]))’,

In the click event I am using

$.ajax({

            'type':'post',


            'url':$(this).attr('href'),


            'success':function(data){


                    $('#collapse4').html(data);exit;


            }


    });

check this part of code




$data["id"]



This should be




$data->id



or try




isset($data["id"])?$data["id"]:0



$data->id is used for CActiveDataProvider. For CSqlDataProvider we should use $data[“id”]. Also problem is not there, I am getting the id, but is not getting submitted as POST method. Instead ‘id’ is passed as query string along with the url.

This is the full code for grid view

$this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=>'my-grid',


'itemsCssClass'=>'table table-bordered table-condensed table-hover table-striped dataTable',


'filter'=>$model,


'dataProvider'=>$model->selectAll(),


'enablePagination' => true,


'pagerCssClass'=>'dataTables_paginate paging_bootstrap',


'pager' => array('header'=>'','htmlOptions'=>array('class'=>'pagination')),


'columns' => array(


        'brand_id',


        'brand_name',


		'brand_code',


        array(


            'class' => 'CButtonColumn',


        	'template'=>'{update} {delete}',


        	'buttons'=>array(


			'update'=>array(


        	'url'=>'$this->grid->controller->createUrl("/controller/action",array("brand_id"=>$data["brand_id"]))',


        	'options'=>array( 


        			 'ajax'=>array(


                            'type'=>'get',


        					'url'=>"js:$(this).attr('href')", 


        					'dataType'=> 'json',


                            'success' => 'js:function(data) { 


                            $("#ItemBrand_brand_name").val(data.brand_name);


                            $("#ItemBrand_brand_code").val(data.brand_code);


                    		$("#ItemBrand_editmode").val(1);


                    		$("#ItemBrand_vrbrand_id").val(data.brand_id);


                    		exit;


                            }',


                           )),


                           'label' => '<i class="glyphicon glyphicon-edit"></i>',


            'imageUrl' => false,


			


			'visible'=>'true;'


		),


		'delete'=>array(


        	'url'=>'$this->grid->controller->createUrl("/controller/action",array("brand_id"=>$data["brand_id"]))',


        	'options'=>array(''),


			'click'=>"function(){


									$.ajax({


            						'type':'get',


            						'url':$(this).attr('href'),


            						'success':function(data){


                    				$.fn.yiiGridView.update('my-grid');exit;


            					},


            						'error':function(xhr, status, error){


                    				alert(xhr.responseText);exit;


            					}


    							});


									return false;


									}",


			'label' => '<i class="glyphicon glyphicon-remove"></i>',


            'imageUrl' => false,


			'visible'=>'true;'


		),


		),


        ),


        ),

))

hello candyman you forget to change the type of ajax submission to post so that they submitting the values in ajax via get method only

here i highlighted your mistake

‘update’=>array(

        	'url'=>'$this->grid->controller->createUrl("/controller/action",array("brand_id"=>$data["brand_id"]))',


        	'options'=>array( 


        			 'ajax'=>array(


                            [b]'type'=>'get',[/b]


        					'url'=>"js:$(this).attr('href')", 


        					'dataType'=> 'json',


                            'success' => 'js:function(data) { 


                            $("#ItemBrand_brand_name").val(data.brand_name);


                            $("#ItemBrand_brand_code").val(data.brand_code);


                    		$("#ItemBrand_editmode").val(1);


                    		$("#ItemBrand_vrbrand_id").val(data.brand_id);


                    		exit;


                            }',


                           )),


                           'label' => '<i class="glyphicon glyphicon-edit"></i>',


            'imageUrl' => false,


			


			'visible'=>'true;'


		),

that is the mistake so change it like this ‘type’=>‘POST’,

Sorry. It was a mistake when I pasted the code here. Actually I tried with POST method. Any way I solved the problem by adding the ID to the ‘<a>’ tag of the button and then posted the id attribute of that tag.