Obatining values from drop down list

I cannot get the value of my drop down list item in my controller.

View File Snippet




<div class="row">

	<?php echo $form->label($model,'reel'); ?>

	<?php echo $form->dropDownList($model,'reel',array(

	'Casting Director'=>'Casting Director $89',

	'Actor'=>'Actor Web File $149',

	'Actor DVD'=>'Actor Web File + DVD $179',

	'DVD'=>'DVD Demo $239',

	'DVD Reel'=>'DVD Demo + Actor Reel $299')); ?>

</div>



Controller Snippet




foreach($model->attributes=$_POST['Order'] as $name=>$value)

	echo "$name $value<br />";


echo $model->reel;



The foreach loop will print the name value pairs fine. If I try to assign $model->reel to a variable or even just try echo $model->reel, I get nothing.

What am I doing wrong?

Thanks.

first you need to instantiate your model ($model = new Order;) and then you have to assign $model->attributes=$_POST[‘Order’] outside of the foreach loop. Then you should be able to print $model->reel

Only posted a snippet of the controller, here’s the full contoller




public function actionIndex(){

	$model=new Order;


	// Uncomment the following line if AJAX validation is needed

	$this->performAjaxValidation($model);


	if(isset($_POST['Order'])){

		$model->attributes=$_POST['Order'];

		if($model->validate()){

			foreach($model->attributes=$_POST['Order'] as $name=>$value)

		echo "$name $value<br />";

		$session = Yii::app()->session->getSessionID();

		//echo $session;

		echo $model->sub;

	}

}

	$this->render('index',array('model'=>$model,));

}




I can echo $model->variable on all form variables except the drop down list.

Posting your model code would be of much help; but in meantime, I have a guess: Do you have any validation rules for reel attribute?

No validation done on the variable reel for the drop down. There is one selected by default.

Model




<?php


/**

 * This is the model class for table "orders".

 *

 * The followings are the available columns in table 'orders':

 * @property integer $id

 * @property string $php_session

 * @property integer $orderID

 * @property string $first_name

 * @property string $last_name

 * @property string $email

 * @property string $item_name

 * @property integer $item_amount

 * @property string $payer_first_name

 * @property string $payer_last_name

 * @property string $payer_email

 * @property string $payment_status

 * @property integer $subscribe

 */

class Order extends CActiveRecord

{

	public $firstName;

	public $lastName;

	public $email;

	//public $os0;

	public $reel;

	public $sub;

	

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Order the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'orders';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			// first name, last email, email are required

			array('firstName, lastName, email', 'required'),

			// email has to be a valid email address

			array('email', 'email'),

			array('sub','compare','compareValue'=>'-1','operator'=>'>','message'=>'You must choose if you want to receive information from Hudson Reels.'),

		);

	}


	/**

	 * @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',

			'php_session' => 'Php Session',

			'orderID' => 'Order',

			'first_name' => 'First Name',

			'last_name' => 'Last Name',

			'email' => 'Email',

			'item_name' => 'Item Name',

			'item_amount' => 'Item Amount',

			'payer_first_name' => 'Payer First Name',

			'payer_last_name' => 'Payer Last Name',

			'payer_email' => 'Payer Email',

			'payment_status' => 'Payment Status',

			'subscribe' => 'Subscribe',

			'reel'=>'Reel Type',

			'sub'=>'Stay Informed',

		);

	}




}



I added the variable to my required rule and now I can access it using $model->reel. All form fields, except hidden fields, need to be specified in the model rules() to access them via $model->name?

All form fields, even hidden ones need to have validation rules, or they won’t be assigned to the model in an assignment of following format:


$model->attributes = $_POST['modelClassName']

If a field does not need validation of any type, define it as safe:


public function rules() {

    return array(

        ...

        array('reel', 'safe'),         

    );

}

or you can assign it manually in controller code:


$model->reel = $_POST['modelClassName']['reel'];