Yii Framework Forum: Passing Selected Item In Dropdown To Controller Using Ajax, Htmloption, How ? - Yii Framework Forum

Jump to content

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

Passing Selected Item In Dropdown To Controller Using Ajax, Htmloption, How ? Rate Topic: -----

#1 User is offline   manhooei 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 13-September 11

Posted 13 January 2013 - 05:32 PM

I have this dropdown list, when I select the item from it the ajax post the item number
to the controller. I don't want the integer number, I actually want the name of the
selected item. So how can I perform something like that ?

here is my code :

echo $form->dropDownList($model,'user_country',$countryList,
array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('signup/dynamicstates'),
'update' => '#User_user_region'
)
));

I want the post method sends the select country name in the list "USA" instead of 0 or 1 or any digit.
0

#2 User is offline   ktwbc 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 06-December 11

Posted 13 January 2013 - 05:43 PM

How is $countryList being built? If you want a text value, it's simplest just to build it there.

For example, you could actually reference back a function from your model to dynamically get the list, such as replacing $countryList with

echo $form->dropDownList($model,'user_country',$model->Countries,
array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('signup/dynamicstates'),
'update' => '#User_user_region'
)
));




then in your model:

	public function getCountries() {

		return CHtml::listData(Country::model()->findAllByAttributes(array('active'=>1),array('order'=>'sort_order,country')), 'country', 'country');

	}


note the 'country','country' instead of a typical 'id','country' so it's using the text label as both the value and the label.
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