Yii Framework Forum: Problem In Multiple Dependent Dropdown - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem In Multiple Dependent Dropdown problem in multiple dependent dropdown Rate Topic: -----

#1 User is offline   rkrram37 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 06-March 13
  • Location:coimbatore,Tamil nadu,India

Posted 15 March 2013 - 03:56 AM

hello everyboby i have state table with state_id and state, city table with city_id,city,state_id.While using the coding from websites hyiihelp.blogspot.in/2012/05/yii-dropdownlist-onchange.html, www.yiiframework.com/wiki/24/creating-a-dependent-dropdown/ i dint get the output, any suggestion please.
0

#2 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 23-January 12
  • Location:Paris - France

Posted 15 March 2013 - 04:12 AM

Hi, give us your code (view and controller) and use the code snippet for that.
0

#3 User is offline   rkrram37 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 06-March 13
  • Location:coimbatore,Tamil nadu,India

Posted 15 March 2013 - 04:20 AM

controller code

<?php
class PlaceController extends Controller
{
public $layout='//layouts/column1';

public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + delete', // we only allow deletion via POST request
);
}

public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('loadcities','index'),
'users'=>array('*'),
)
);
}

public function actionLoadcities()
{
$data=Place::model()->findAll('state_id=:state_id', array(':state_id'=>(int) $_POST['state_id']));

$data=CHtml::listData($data,'city_id','city');

echo "<option value=''>Select City</option>";
foreach($data as $value=>$city)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($city),true);
}

public function actionIndex()
{
$this->render('index');
}

public function loadModel($id)
{
$model=Place::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}

/**
* Performs the AJAX validation.
* @param User $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='place-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}

}
?>


model code
<?php
class Place extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}

/**
* @return string the associated database table name
*/
public function tableName()
{
return 'state';
}
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('state,state_id', 'required'),
array('state,state_id', 'length', 'max'=>20),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('state,state_id', 'safe', 'on'=>'search'),
);
}

/**
* @return array relational rules.
*/
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(
);
}

/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'state' => 'State',
'state_id' => 'State Id'
);
}

/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.

$criteria=new CDbCriteria;

$criteria->compare('state_id',$this->state_id);
$criteria->compare('state',$this->state,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
?>

view Code

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'place-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,'state_id'); ?>
<?php echo $form->dropDownList($model,'state_id',
array(1=>'tamilnadu',2=>'karnataka'),
array(
'prompt'=>'Select Region',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadcities'),
'update'=>'#city',
'data'=>array('state_id'=>'js:this.value'),
)));

echo CHtml::dropDownList('city','', array(), array('prompt'=>'Select City')); ?>
<?php echo $form->error($model,'state_id'); ?>
</div>


<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

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

</div>
1

#4 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 23-January 12
  • Location:Paris - France

Posted 15 March 2013 - 04:52 AM

As you didn't use snippet (<>) for your code, this is not very easy to follow.
You can try to debug it, check that the actionLoadcities is launch, then check if the controller return correct datas.
0

#5 User is offline   rkrram37 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 06-March 13
  • Location:coimbatore,Tamil nadu,India

Posted 15 March 2013 - 07:08 AM

In PlaceController actionLoadcities() i got the error Undefined index: state_id
0

#6 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 23-January 12
  • Location:Paris - France

Posted 15 March 2013 - 09:42 AM

add
var_dump($_POST['state_id']));

at the beginning of the controller for debug
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users