Yii Framework Forum: dependent dropdown lists - Yii Framework Forum

Jump to content

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

dependent dropdown lists selection of city

#1 User is offline   Karthik12 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 15-March 11

Posted 26 May 2011 - 02:22 AM

Hello ,
am new to this,working on dependent dropdown,when i select state i want show city names,here am send sample code
<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'worldareascities-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,'name'); ?>
			<td><?php echo $form->dropDownList($model,'name', CHtml::listData(worldareascities::model()->findAll(), 'name', 'name'),array('prompt'=>'') ); ?></td>
 	        <?php echo $form->error($model,'name'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'stateid'); ?>
		<td><?php echo $form->dropDownList($model,'stateid', CHtml::listData(worldareascities::model()->findAll(), 'stateid', 'stateid'),array('prompt'=>'') ); ?></td>

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

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

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

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

0

#2 User is offline   Mayur Ahir 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 08-July 11

Posted 08 July 2011 - 03:25 AM

//In view file
<td>
    <?php
    echo
    $form->dropDownList(
            $model, 'country', DropdownHelper::getOptionAll(
                    'Country', 'id', 'name'
            ), array(
        'prompt' => '-Select Country-',
        'onchange' => CHtml::ajax(
                array(
                    'type' => 'POST',
                    'url' => CController::createUrl('dynamicCity'),
                    'update' => '#' . CHtml::activeId($model, 'city')
                )
        )
            )
    );
    ?>
</td>
<td>
    <?php
    echo
    $form->dropDownList(
            $model, 'city', array(), array(
        'onchange' => CHtml::ajax(
                array(
                    'type' => 'POST',
                    'url' => CController::createUrl('dynamicArea'),
                    'update' => '#' . CHtml::activeId($model, 'area')
                )
        )
            )
    );
    ?>
</td>
//My model name is Location
//In controller
public function actionDynamicCity() {
        $country_id = $_POST['Location']['country'];

            $data = Country::model()->findByPk($country_id);
            $i = 0;
            foreach ($data->city as $row) {
                $list[$i]['id'] = $row['id'];
                $list[$i]['city_name'] = $row['city_name'];
                $i++;
            }

            $data = CHtml::listData($list, 'id', 'city_name');
            echo CHtml::tag('option', array('value' => 0), CHtml::encode('-Select City-'), true);

            foreach ($data as $value => $key) {
                echo CHtml::tag('option', array('value' => $value), CHtml::encode($key), true);
            }
    }

0

#3 User is offline   parmesh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 10-June 12
  • Location:pune

Posted 11 June 2012 - 12:03 AM

hi,
i am new to this framework.
i want to get city name from dropdown if i select state name.
i am having table for city name and state name.
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