Hi All,
I’m trying to populate the ‘id’ textField based on a data from ‘department_id’ and ‘facility_id’ dropDownList.
Using this form;
<?php
/* @var $this ScaffoldController */
/* @var $model Scaffold */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'scaffold-form',
	'enableAjaxValidation'=>false,
        'htmlOptions' => array('enctype' => 'multipart/form-data')
     )); ?>
	<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,'department_id'); ?>
		<?php echo $form->dropDownList($model,'department_id', Department::model()->getDepartmentOptions(), array('prompt'=>'Select Department')); ?>
		<?php echo $form->error($model,'department_id'); ?>
	</div>
        
        <div class="row">
		<?php echo $form->labelEx($model,'facility_id'); ?>
		<?php echo $form->dropDownList($model,'facility_id', Facility::model()->getFacilityOptions(), array('prompt'=>'Select Facility')); ?>
		<?php echo $form->error($model,'facility_id'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'id'); ?>
		<?php echo $form->textField($model,'id',array('size'=>11,'maxlength'=>7)); ?>
		<?php echo $form->error($model,'id'); ?>
	</div>
...
(Code suggested by a friendly Yii Forum Member)
Controller;
public function actionNewReferenceId()
            {
                $departmentId=null;
                $facilityId=null;
                if(isset($_GET['departmentId']))
                    $departmentId = $_GET['departmentId'];
                if(isset($_GET['facilityId']))
                    $facilityId = $_GET['facilityId'];
                //generateRef is the function in $model that calculates the new reference based on the department and facility 
                $ref = Scaffold::generateRef($departmentId,$facilityId);
                if($ref>0)
                     echo CJSON::encode(
                        array(
                            'status'=>'success', 
                            'newReferenceID'=>$ref,
                    ));
                else
                     echo CJSON::encode(
                        array(
                            'status'=>'error',
                            'message'=>'Invalid reference generated', 
                            'newReferenceID'=>$ref,
                    ));
            }
Model;
/*
         * returns generated reference if reference generated successfully, if not it returns 0
         */
        public static function generateRef($department_id,$facility_id){
                $newReference = 0;
                if($department_id && $facility_id){
                        $deptExists=Department::model()->exists('id=:id',array(':id',$department_id));
                        $facExists =Facility::model()->exists('id=:id',array(':id',$facility_id));
                        if($deptExists && $facExists){
                                $sql='SELECT max(id) as lastRef from tbl_scaffold where department_id=:dept_id and facility_id=:fac_id'; 
                                $command=Yii::app()->db->command($sql);
                                $command->bindParam(':dept_id',$department_id,PDO::PARAM_INT);
                                $command->bindParam(':fac_id',$facility_id,PDO::PARAM_INT);
                                $result= $command->queryRow();
                                if($result)
                                        $newReference =$result['lastRef']+1;
                                else{
                                        switch($department_id){
                                                case 1: //Ops & Maint
                                                        if($facility_id==1) //CUQ
                                                                $newReference=10001;
                                                        else if($facility_id==2) //DPP
                                                                $newReference=30001;
                                                         break;
                                                case 2: //AIMS
                                                        if($facility_id==1)
                                                                $newReference=50001;
                                                        else if($facility_id==2)
                                                                $newReference=70001;
                                                        break;
                                                case 3: //Shutdown
                                                        if($facility_id==1)
                                                                $newReference=80001;
                                                        else if($facility_id==2)
                                                                $newReference=90001;
                                                        break;
                                        }
                                }
                        }
                }
                return $newReference;
        }
View;
<?php 
Yii::app()->clientScript->registerScript("newreference","
        $('#Scaffold_department_id, #Scaffold_facility_id').change(function(e){
        
                        var departmentId = $('#Scaffold_department_id').val();
                        var facilityId= $('#Scaffold_facility_id').val();
        
                        if(departmentId && facilityId){
                        var info = { departmentId:departmentId,
                        facilityId:facilityId
        };
        
                        $.ajax({
                        url: '" . Yii::app()->createUrl("/scaffold/newReferenceId") . "',
                        data: info,
                        type: 'GET',
                        dataType: 'json',                      
                        success:function(data){
                        if(data.status=='success')
                        $('#Scaffold_id').val(data.newReferenceID);
        },
        });
        
        }
        });
");
?>
The script is being attached and the error is on PHP but I don’t know how to fix it. Please HELP.