Dependent dropdown data not coming

I have a dropdown list which takes data from "supercategorymaster" table to store in the "maintable" table.

When the value for this is selected I want another dropdown to be filled which will get data from "subcategorymaster" table which when filled up will save in "maintable" table.

The “subcategory” master table contains ‘supercategory_id’,‘supercategory_name’,‘supercategory_id’

In my register view:




<div class="row">

		<?php echo $form->labelEx($maintable,'supercategory'); ?>

		<?php 

		

		$supercategorydrop=CHtml::listData(SupercategoryMaster::model()->findAll(), 'supercategory_id', 'supercategory_name');

		echo $form->dropDownList($maintable,'supercategory', $supercategorydrop,

		

		 array(

                                'ajax' => array(

                                'type'=>'POST',

                                'url'=>CController::createUrl('subcategorymaster/subcategorycat'),

                                'update'=>'#subcategory')));

		 ?>

		<?php echo $form->error($maintable,'religion'); ?>

	</div>

	

	

	

	<div class="row">

		<?php echo $form->labelEx($maintable,'subcategory'); ?>

		<?php

		   echo $form->dropDownList($maintable,'subcategory',array());

            		?>

		<?php echo $form->error($maintable,'subcategory'); ?>

	</div>

	

	

In the Controller for the subcategory(SubcategoryMaster) which picks up data from subcategory table




public function actionsubcategorycat()

	{

		 $data=SubcategoryMaster::model()->findAll('parent_id=:parent_id',

		 

             array(':parent_id'=>(int) $_POST['supercategory']));


             $data=CHtml::listData($data,'supercategory_id','supercategory_name');

              foreach($data as $value=>$subcategory)

                {

                    echo CHtml::tag('option',

                               array('value'=>$value),CHtml::encode($subcategory),true);

                }

			}






When I go to http://localhost/yiiitest/index.php?r=subcategorymaster/subcategorycat

I get following error

[b]PHP Error

Undefined index: supercategory[/b]

I have given access rules:

[b]‘actions’=>array(‘index’,‘view’,‘castecat’),

			'users'=&gt;array('*'),[/b]

How can I get the data in teh second dropdown

Please use the code directive so that your code is more readable (<> on the editor toolbar)

For your error check the line where this error is reported I presume it’s this one:


<?php echo $form->labelEx($maintable,'supercategory'); ?>

This looks for the attribute $model->supercategory… and it seems you don’t have it .

Sorry About that

The error shows

PHP Error

Undefined index: supercategory

C:\wamp\www\yiiitest\protected\controllers\SubcategoryMasterController.php(60)




48     

49     

50     

51     

52 public function actionsubcategorycat()

53     {

54 

55         

56         

57         

58          $data=SubcategoryMaster::model()->findAll('parent_id=:parent_id',

59          

60              array(':parent_id'=>(int) $_POST['supercategory ']));




That means that the post array does not have this value… could be that you did not declare that attribute as safe ?

I have checked and it is safe

Could it be because there is no supercategory in "subcategorymaste" table

"subcategorymaste" table only has

‘supercategory_id’,‘supercategory_name’,‘subcategory_id’

If this is the situation how can I solve it without changing the database

If you don’t have that filed you should have got the error "Property “<yourmodel>.supercategory” is not defined. "

Check again all your code… you need to use an existing attribute… be it a field name or declared in the model class…

Hi,

I have a dependent dropdown containing religion and caste. The dropdown is working properly ie. when i select religion the caste corresponding to the religion are displayed, but only the religion information is stored in the database and the caste field is empty. The caste data is not stored in the database.

Thanks,

I have now been able to retrieve the data in the dependent dropdown, but I am getting a similar problem when saving the data.

Any Ideas?

Plz suggest some solution.

Thanks,

I have a dependent dropdown on the home page that is on views/site/index.php page it is not showing values when parent item is changed.

Is there some issue when placing a dependent dropdown on views/site/index.php