(sloved)can not get value use $_GET[] or $_POST[] in controller

Hi my friends:

I get some problem, anyone can help me?I found many topics in forums but still not sloved.

I use CCeckBoxColumn on every row of the items on grid list view,the code is:

view: email/search




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider'=>$search->search(),			

	'summaryText'=>Yii::t('email','结果:{start}-{end} of {count}'),

	'selectableRows'=>2,					

	'columns'=>array(				

		array(											

		    'class'=>'myCheckBoxColumn',					

                    'id'=>$search->email_id,					

                    'visible'=>(Yii::app()->user->getState("role")) == User::ADMIN,

                    'checked'=>'(in_array($data->email_id,Yii::app()->session["ids"]))?true:false',

               ),

	       'email_date',									

		'email_from',									

		'email_to',					

		'email_theme',	

	)); 

         ?>				

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



In order to use CGriedViews selection feature,i add a little javascript to get the current selection any time(eg: when user click someone checkbox).The javascript in the view ‘email/search’ .




<script type="text/javascript">

    $(document).ready(function(){

	$("[name='yw1_c0[]']").click(function(){

            alert(this.id);

            var id = this.id;

	    $.ajax({

                async:false,

                url:'http://localhost/email/index.php?r=email/search',

		type:'GET',

		data:'ids='+id,

		dataType:'text',

		timeout:1000,

		error:function(){

			alert('Error!');

		},

		success:function(){

                    alert('success!');

		}

	    });

       });

   });

</script>


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider'=>$search->search(),			

	'summaryText'=>Yii::t('email','结果:{start}-{end} of {count}'),

	'selectableRows'=>2,					

	'columns'=>array(				

		array(											

		    'class'=>'myCheckBoxColumn',					

                    'id'=>$search->email_id,					

                    'visible'=>(Yii::app()->user->getState("role")) == User::ADMIN,

                    'checked'=>'(in_array($data->email_id,Yii::app()->session["ids"]))?true:false',

               ),

	       'email_date',									

		'email_from',									

		'email_to',					

		'email_theme',	

	)); 

         ?>				

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



Controller:email/search




public function actionSearch(){	

       print_r($_GET['ids']);

        Yii::app()->session['ids'] = array(123,234,236);

       	$search = new Email('search');

        $folder_object = new Folder;

	$folder_models = Folder::model()->findAll();      

		if(count($folder_models) != 0){

			foreach($folder_models as $folder_model){

				$folder[$folder_model->folder_id] = $folder_model->folder_name;

			}

		}else{

			$folder = array();

		}		

		

	 	$this->render('search',array('search'=>$search,'folder'=>$folder));	

	}



In the controller use $_GET[‘ids’] can not get id of selected checkbox,is wrong:




Undefined index: ids



Thanks for help!

Is the javascript function executed?

Do you get the alert()?

what is the value of id (this.id)

Try to use firebug to see the URL that is called and to see the parameters that are passed

thanks you very much, mdomba!

I sloved it! :rolleyes: Without your help I could not solve! :lol: Thanks again

Would be nice… for other that happen to read this post… that you write the solution that is working…

Thank you! Yes




<script type="text/javascript">

    $(document).ready(function(){

        $("[name='yw1_c0[]']").click(function(){

            alert(this.id);

            var id = this.id;

            $.ajax({

                async:false,

                url:'http://localhost/email/index.php?r=email/search',

                type:'GET',

                data:'ids='+id,

                dataType:'text',

                timeout:1000,

                error:function(){

                        alert('Error!');

                },

                success:function(){

                    alert('success!');

                }

            });

       });

   });

</script>



The above code is corrected.


Try to use firebug to see the URL that is called and to see the parameters that are passed



This my method of what you tell me.Thanks again! mdomba

Cause of the problem is:

In a action,you shoud not render a view!For example:




public function actionSearch(){ 

       print_r($_GET['ids']);

        Yii::app()->session['ids'] = array(123,234,236);

        $search = new Email('search');

        $folder_object = new Folder;

        $folder_models = Folder::model()->findAll();      

                if(count($folder_models) != 0){

                        foreach($folder_models as $folder_model){

                                $folder[$folder_model->folder_id] = $folder_model->folder_name;

                        }

                }else{

                        $folder = array();

                }                                                

        }



So you should write a function to receive parameters,only deal with but not render a view!

Hope can help other pepole! :rolleyes:

I’m very sorry if you do not understand what I mean :unsure: ,my english is not very good! :blink:

Thanks again! mdomba :rolleyes: