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