How to retrieve selected activeCheckBox

Hi everyone. I have a difficulty in retreiving the selected activeCheckBox.

Here is the detail :

Member can subscribe to several categories, for example : sport, technology and others.

The relation between member and category is "categories".

Now, in profile page, in case he/she wants to change the category, he/she can click the desired category and unclick the old one.

Here is the code :




<?php echo CHtml::activeCheckBoxList(

                                     $model,

                                     'categoryIds',

                                     Chtml::listData(BusinessCategory::model()->findAll(),'bc_id','bc_name'));?>

Using the above code, it will automatically tick the category that the member has subscribed before. Now, if I untick the old one, then tick the other category, then submit, my new input does not have any impact. Meaning when i echo $member[‘categoryIds’] it still show the old data, not the ones that i just ticked. Where do i do wrong? Thanks.

Maybe your form is not saved. Maybe you have some validation errors.

Could be a tipo… in the view you have $model->categoryIds but later you wrote that you are echoing $member->categoryIds

Hi Putera,

Can you show me the controller part, i think it must be some part wrong in your controller. :)

I’m sorry for the late reply.

So,here is the more detailed case. One member can subscribe to several category, and the category can have several member attached to it. Which means the relations is many to many. So I created a junction table tbl_subscribed_member.

This is my member.php which is the model :





class Member extends VeevouAR

{

       ......


       public $categoryIds;


       public function afterFind() //Used to retrieve the categories of the selected member

       {

          $this->categoryIds = array_keys($this->categories);

          parent::afterFind();

       }


       public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			

	        'categories' => array(self::MANY_MANY, 'BusinessCategory', 'tbl_subscribed_member(member_id, bc_id)','index'=>'bc_id'),

                        

		);

	}


       ........


}




And this is my BusinessCategory.php :





public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'members' => array(self::MANY_MANY, 'Member', 'tbl_subscribed_member(bc_id, member_id)'),

		);

	}




This is my _update.php





<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'member-form',

	'enableAjaxValidation'=>false,

)); ?>


<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'member_username'); ?> <br/>

		<?php echo $form->textField($model,'member_username',array('size'=>20,'maxlength'=>20)); ?>

		<?php echo $form->error($model,'member_username'); ?>

	</div>


        <div class="row">

                Please choose your category :

		<?php echo CHtml::activeCheckBoxList(

                                     $model,

                                     'categoryIds',

                                     Chtml::listData(BusinessCategory::model()->findAll(),'bc_id','bc_name'));?>

	</div>




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


</div><!-- form -->




and this is my controller :





public function actionUpdate($id)

{

            $member = $this->loadModel($id);

            

            // Uncomment the following line if AJAX validation is needed

            // $this->performAjaxValidation($model);


            

            if(isset($_POST['Member']))

            {

                $member->attributes = $_POST['Member'];


                print_r($member['categoryIds']);

            }

}






Now, when i do this, it only show the old value, not the one that i just ticked. There must be some mistake but dunno where it is. Any advice? =) Thanks

And this is my HTML of _update.php:





<div class=row">


<input id="ytMember_categoryIds" type="hidden" value="" name="Member[categoryIds]" /><input id="Member_categoryIds_0" value="1" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_0">Food & Beverages</label><br/>

<input id="Member_categoryIds_1" value="2" checked="checked" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_1">Health & Beauty</label><br/>


<input id="Member_categoryIds_2" value="3" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_2">Fashion & Appearel</label><br/>

<input id="Member_categoryIds_3" value="4" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_3">Electronic</label><br/>

<input id="Member_categoryIds_4" value="5" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_4">Travel & Hotel</label><br/>

<input id="Member_categoryIds_5" value="6" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_5">Others</label><br/>

<input id="Member_categoryIds_6" value="7" checked="checked" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_6">All</label>


</div>


<div class="row">

		<label for="Member_member_zip">Zip</label> <br/>


		<input size="5" maxlength="5" name="Member[member_zip]" id="Member_member_zip" type="text" value="11320" />			</div><div class="row">

		<label for="Member_member_zip">Zip</label> <br/>


		<input size="5" maxlength="5" name="Member[member_zip]" id="Member_member_zip" type="text" value="11320" />			</div>






It seems that the data are not sent correctly. Why is there any "yt" in front of the id ?


<input id="ytMember_categoryIds" type="hidden" value="" name="Member[categoryIds]" />

I’m still relatively new to yii. Please advice =) Thanks

Hi Putera,

I think the problem should be occur from this:




$member = $this->loadModel($id);



Why your form don’t have any submit button. Do you have try to use javascript check for the checkbox result before onSubmit




$form=$this->beginWidget('CActiveForm', array(

	'id'=>'seller-form',

	'enableAjaxValidation'=>true,

	'htmlOptions'=>array('enctype'=>'multipart/form-data','name'=>'productListing','OnSubmit'=>'return CheckForm();'),

));



Try to see is is it still same with the previous tick value.

In the controller, i prefer to check like this




public function actionUpdate($id)

{

         

    if(isset($_POST['Member']))

    {

     $member->attributes = $_POST['Member'];

     print_r($member['categoryIds']);

    }

}



should be return and array data.

After I check the HTML it looks like this :




<div class="row">


<input id="ytMember_categoryIds" type="hidden" value="" name="Member[categoryIds]" />


<input id="Member_categoryIds_0" value="1" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_0">Food & Beverages</label><br/>


<input id="Member_categoryIds_1" value="2" checked="checked" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_1">Health & Beauty</label><br/>


<input id="Member_categoryIds_2" value="3" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_2">Fashion & Appearel</label><br/>


<input id="Member_categoryIds_3" value="4" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_3">Electronic</label><br/>


<input id="Member_categoryIds_4" value="5" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_4">Travel & Hotel</label><br/>


<input id="Member_categoryIds_5" value="6" type="checkbox" name="Member[categoryIds][]" /> 

<label for="Member_categoryIds_5">Others</label><br/>


<input id="Member_categoryIds_6" value="7" checked="checked" type="checkbox" name="Member[categoryIds][]" /> <label for="Member_categoryIds_6">All</label>	

	                

</div>




I dont know why, but there is extra "[ ]" after the text field name.

So, in my controller, when i do this :





echo $member['categoryIds'];




it still shows the old value, not the ones that I just ticked.

Compare with this :





<div class="row">


<label for="Member_member_gender">Gender</label>  

              

<div class="compactRadioGroup">


<input id="ytMember_member_gender" type="hidden" value="" name="Member[member_gender]" />   

    

<input id="Member_member_gender_0" value="1" checked="checked" type="radio" name="Member[member_gender]" /> <label for="Member_member_gender_0">Male</label> 


<input id="Member_member_gender_1" value="0" type="radio" name="Member[member_gender]" /> 

<label for="Member_member_gender_1">Female</label>                


</div>


</div>




There is no extra "[ ]" after the field name. Is it possible this is the problem? Because when I check using javascript :





function checkUpdateProfile() {

   

   var buttonGroups = document.getElementsByName('Member[categoryIds][]');

   var cat = false;


   for (var i=0; i < buttonGroups.length; i++) {

          if(buttonGroups[i].checked)

          {

              alert (buttonGroups[i].value);

              cat = true;

          }

   }


    if(cat==false){

        alert('Please Choose at least one category');

        return false;

    }

    else

    {

          return true;

    }  

} 




This javascript will read all the checkboxes, but when I use this :





function checkUpdateProfile() {

   

   var buttonGroups = document.getElementsByName('Member[categoryIds]'); //Without extra bracket

   .......

}



This reads nothing. Any idea about this ? :) Thanks

Do you have




    ...

    array('categoryIds', 'safe'),

    ...



in your model rules?

@maschingan

Wow, thanks so much. Now it works well. Thanks a lot :)