Dropdown Dependant Textfield

Hi all,

This is my first post. please bear with me for some time.

I want to show some text after user selects from dropDown list. Both my dropDownList and text that has to be displayed comes from same table.

I have achieved this by using CHtml,ajax. But the problem is- when the form is submitted, it gives me an error that the field is empty.

this is code snippet of form:




<div class="row">

            <?php echo $form->labelEx($model, 'location_of_pickup'); ?>

            <?php echo CHtml::dropDownList('location_of_pickup','',ContactDetails::model()->getLocationIdOptions(),

                                            array('ajax' =>

                                            array('type' => 'POST',

                                                   'url' => CController::createUrl('loadlocation'),

                                                   'update' => '#location',

                                                 )));?>      

                                    

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


                                

                  <div id="location">

                    

                  </div>

        </div>



This is the function getLocationIdOptions() which calls model ContactDetails,




public function getLocationIdOptions() {

        return CHtml::listData(ContactDetails::model()->findAll(), 'current_loc_id', 'current_loc_id');

}



This is the controller snippet




public function actionLoadlocation() {

                $ContactDetails= new ContactDetails;    

                $connection=Yii::app()->db;

              

                $sql='select current_address from  Contact_Details where current_loc_id= "'.$_POST['location_of_pickup'].'"';

                $command=$connection->createCommand($sql);

                $value=$command->queryScalar();

        echo $value;




if i change the code in the view CHtml:: to $form->, no error in the form but the text is not displayed.

i am getting this error in firebug

500 Internal Server Error.

<h1>PHP Error [8]</h1>

<p>Undefined index: location_of_pickup …

The form code after i change CHtml:: to $form-> is




<div class="row">

            <?php echo $form->labelEx($model, 'location_of_pickup'); ?>

            <?php echo $form->dropDownList($model,'location_of_pickup',ContactDetails::model()->getLocationIdOptions(),

                                            array('ajax' =>

                                            array('type' => 'POST',

                                                   'url' => CController::createUrl('loadlocation'),

                                                   'update' => '#location',

                                                 )));?>      

                                    

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

               

                  <div id="location">

                    

                  </div>

        </div>



Please suggest me a way to fill the field with the selected value.

If you anything apart this please ask.

Thanks

Hi

public function actionLoadlocation() {

        &#036;criteria = new CDbCriteria;


        &#036;criteria-&gt;select = 't.current_address;


        &#036;criteria-&gt;addCondition(&quot;current_loc_id='&quot;.&#036;_POST['location_of_pickup']'.&quot;&quot;);


        &#036;resultSet    =    ContactDetails::model()-&gt;find(&#036;criteria);

echo $resultSet ->current_address;

}

Thanks for the reply ankit and apologies for late reply(that was Friday night)…

anyways i went on debugging and i came to know that if we use $form or CHtml::activeDropDownList, the posted value is an array. so we have to check as $_POST[your-model-name][your-form-attribute-name] in the controller.

Here’s my working code i am posting this for anybody who is in search.

The controller snippet:




public function actionLoadlocation() {     

                $connection=Yii::app()->db;

                if(isset($_POST['RfcSubmission']['location_of_pickup'])){

                  $postval= $_POST['RfcSubmission']['location_of_pickup']; 

                        $sql='select current_address from  Contact_Details where current_loc_id= "'.$postval.'"';

                        $command=$connection->createCommand($sql);

                        $vaue=$command->queryScalar();

                        echo $vaue;

                }

            else{

                   echo 'Error retrieving from db';

                 }

        }



The form snippet, here i have used CHtml::activeDropDownList:




<div class="row">

            <?php echo $form->labelEx($model, 'location_of_pickup'); ?>

            <?php echo CHtml::activeDropDownList($model,'location_of_pickup',ContactDetails::model()->getLocationIdOptions(),

                                            array('ajax' =>

                                            array('type' => 'POST',

                                                   'url' => CController::createUrl('loadlocation'),

                                                   'update' => '#location',

                                                 )));

                            ?>                            

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


                                

                  <div id="location">

                      <?php //To display the address of the selected location. ?>  

                  </div>

        </div>



and code for getLocationIdOptions() which calls model ContactDetails is same as above.

Hope somebody may find this useful. cheers…