Autofill form when choose value from dropdown list


(fb) #1

Hi, I have a question to ask.

Capture11

Currently the three dropdown menu from three different table and each have different id called aircraft_type, aircraft_sn and aircraft_reg.

Table aircraft_sn
Captureg

Now, I want to use only table aircraft_type and aircraft_sn, while aircraft_reg data is located in table aircraft_sn. What I want to do is, when user choose value from aircraft type, the aircraft serial number will appear for those aircraft type and aircraft registration will automatically fill when user choose among that serial number.

So far the code I have is:
1)_form.php (project form)

<div class="form-group <?php echo (!is_null($model->getError('aircraft_type_id')))? 'has-error':''; ?>">
					<label class="col-md-3 control-label" for="nama"><?php echo $form->labelEx($model,'aircraft_type_id'); ?> </label>
					<div class="col-md-7">
						<?php //echo $form->textField($model,'aircraft_reg_id',array('size'=>20,'maxlength'=>20,'class'=>'form-control input-md')); ?>
						<?php
						 	$list = CHtml::listData($modelAircraftType, 'id', 'name');
				            echo CHtml::dropDownList('aircraft-type-id-select', '', $list,
				                array(
				                	'class'=>'form-control m-b',
				                	'empty' => 'Select Type...',
				                    'options' => array($model->aircraft_type_id => array('selected' => 'selected')),
				                    'required' => 'required',
				                    //'class' => 'col-md-12', 
				                    //'style'=>'width: 290px',
				                )
				            );
						?>
						<?php echo $form->error($model,'aircraft_type_id'); ?>
					</div>
					<div class="col-md-2"></div>
				</div>

				<div class="form-group <?php echo (!is_null($model->getError('aircraft_sn_id')))? 'has-error':''; ?>">
					<label class="col-md-3 control-label" for="nama"><?php echo $form->labelEx($model,'aircraft_sn_id'); ?> </label>
					<div class="col-md-7">
						<?php //echo $form->textField($model,'aircraft_sn_id',array('size'=>20,'maxlength'=>20,'class'=>'form-control input-md')); ?>
						<?php
						 	$list = CHtml::listData($modelAircraftSerielNumber, 'id', 'sn');
				            echo CHtml::dropDownList('aircraft-sn-id-select', '', $list,
				                array(
				                	'class'=>'form-control m-b',
				                	'empty' => 'Select Serial Number...',
				                    'options' => array($model->aircraft_sn_id => array('selected' => 'selected')),
				                    'required' => 'required',
				                    //'class' => 'col-md-12', 
				                    //'style'=>'width: 290px',
				                )
				            );
						?>
						<?php echo $form->error($model,'aircraft_sn_id'); ?>
					</div>
					<div class="col-md-2"></div>
				</div>

				
				<div class="form-group <?php echo (!is_null($model->getError('aircraft_reg_id')))? 'has-error':''; ?>">
					<label class="col-md-3 control-label" for="nama"><?php echo $form->labelEx($model,'aircraft_reg_id'); ?> </label>
					<div class="col-md-7">
						<?php //echo $form->textField($model,'aircraft_reg_id',array('size'=>20,'maxlength'=>20,'class'=>'form-control input-md')); ?>
						<?php
						 	$list = CHtml::listData($modelAircraftRegistration, 'id', 'name');
				            echo CHtml::dropDownList('aircraft-reg-id-select', '', $list,
				                array(
				                	'class'=>'form-control m-b',
				                	'empty' => 'Select Registration...',
				                    'options' => array($model->aircraft_reg_id => array('selected' => 'selected')),
				                    'required' => 'required',
				                    //'class' => 'col-md-12', 
				                    //'style'=>'width: 290px',
				                )
				            );
						?>
						<?php echo $form->error($model,'aircraft_reg_id'); ?>
						<a class="btn btn-success pull-right" title="" href="<?php echo Yii::app()->createUrl('aircraft/create'); ?>" data-original-title="" style="margin-top:10px; margin-right:20px">
						<i class="fa fa-pencil-square-o"></i>
						<span class="menu-title">New Aircraft</span>
						</a>
					</div>
					<div class="col-md-2"></div>
				</div>

2)Controller (ProjectController)

public function actionCreate()
{
	if (isset(Yii::app()->session['company_id']) && !empty(Yii::app()->session['company_id']))
		$activeCompanyId = Yii::app()->session['company_id'];
	else
		$activeCompanyId = Yii::app()->user->company_id;

	$model = new Project;

	$modelClient=Client::model()->findAll();
	$modelProjectPriority=ProjectPriority::model()->findAll();
	$modelAircraftType=AircraftType::model()->findAll();
	$modelAircraftSn=AircraftSn::model()->findAll();
	$modelAircraftRegistration=AircraftRegistration::model()->findAll();
	$modelProjectApproval=ProjectApproval::model()->findAll();

	$model->project_date=date('j M Y');
	
	if(isset($_POST['Project']))
	{
		$model->attributes=$_POST['Project'];
		$model->id = Project::model()->getMax()+1;
		$model->user_id=Yii::app()->user->account_id;

		$tempDate = $model->project_date;
		$model->project_date = date('Y-m-d', strtotime($tempDate));

		$model->client_id = $_POST['client-id-select'];
		$model->project_priority_id = $_POST['project-priority-id-select'];
		$model->aircraft_type_id = $_POST['aircraft-type-id-select'];
		$model->aircraft_reg_id = $_POST['aircraft-reg-id-select'];
		$model->aircraft_sn_id = $_POST['aircraft-sn-id-select'];
		$model->project_status_id = 1;
	
		//Moc No. (format:company-dept/MOC/year/running #)
		$runningNo=Project::model()->count(array(
			'condition'=>'YEAR(created_time)=YEAR(CURRENT_DATE)',
		));	

		if (strlen(Yii::app()->session['company_shortname']) <= 2)
			$ref_no = Yii::app()->session['company_shortname'];
		else
		$ref_no = substr(Yii::app()->session['company_shortname'],0,-2);
		
		$ref_no .= '/PRO';
		$ref_no .= '/'.date('y');
		$ref_no .= '/'.str_pad($runningNo+1, 4, '0', STR_PAD_LEFT);
		$model->project_no = $ref_no;

		date_default_timezone_set("Asia/Kuala_Lumpur");
		$time = date_default_timezone_get()	;
		$time = date('Y-m-d H:i:s');

		$model->created_time = $time;
		$model->updated_time = $time;
		$model->updated_user_id = Yii::app()->user->user_id;

		if($model->save()) {
			
			if (isset($_FILES['DocumentUpload'])) {
				$files = $_FILES['DocumentUpload'];
				$documentUploadPath = Yii::getPathOfAlias('webroot.uploaded.project').'/';

				for ($i=1;$i<=3;$i++){
				$mod_file = 'mod_file'.$i;

				// file upload - mod project
				if(!is_null($files['name'][$mod_file]) && !empty($files['name'][$mod_file])) {
						list($fn, $ext) = explode(".", basename($files['name'][$mod_file]));
						$filename = substr(str_replace(" ", "_", $fn),0,5).'_'.date('Ymd_His').".".$ext;
						$file = $documentUploadPath.$filename;
						if (file_exists($file)) {
		        		unlink($file);
		       			 }

						move_uploaded_file($files['tmp_name'][$mod_file], $file);

						$modelProjectMod = new ProjectDocumentUpload;
						$modelProjectMod->id = ProjectDocumentUpload::model()->getMax()+1;
						$modelProjectMod->project_id = $model->id;
						$modelProjectMod->media_code_id = 1; // Project Mod
						$modelProjectMod->reference_no = 'Project Mod';
						$modelProjectMod->filename = $filename;
						$modelProjectMod->date = $time;

						if ($modelProjectMod->save()) {

							// add into audit trails
							$modelAuditTrail=new AuditTrail;
							$modelAuditTrail->id = AuditTrail::model()->getMax()+1;
							$modelAuditTrail->controller = Yii::app()->controller->id;
							$modelAuditTrail->action = Yii::app()->controller->action->id;
							$modelAuditTrail->account_id = Yii::app()->user->account_id;
							$modelAuditTrail->company_id = $activeCompanyId;
							$modelAuditTrail->department_id = Yii::app()->user->department_id;
							$modelAuditTrail->user_role_id = Yii::app()->user->user_role_id;
							$modelAuditTrail->level = 1;
							$modelAuditTrail->description = 'Add Mod in Project No. '.$model->project_no;
							$modelAuditTrail->created_time = $time;
							$modelAuditTrail->updated_user_id = Yii::app()->user->user_id;
							$modelAuditTrail->updated_time = $time;
							$modelAuditTrail->save();
						}												
					}
				}
			}
		}//tutup if $model->save
			// //add into timeline
			$modelProjectStatusLog=new ProjectStatusLog;
			$modelProjectStatusLog->id = ProjectStatusLog::model()->getMax()+1;
			$modelProjectStatusLog->project_id = $model->id;
			$modelProjectStatusLog->project_status_id = $model->project_status_id;
			$modelProjectStatusLog->updated_user_id = Yii::app()->user->user_id;
			$modelProjectStatusLog->updated_time = $time;
			$modelProjectStatusLog->save();

	 		$this->redirect(array('view','id'=>$model->id));
		// }
	}
	$this->render('create',array(
		'section'=>1,
		'model'=>$model,
		'modelProjectPriority'=>$modelProjectPriority,
		'modelClient'=>$modelClient,
		'modelAircraftRegistration'=>$modelAircraftRegistration,
		'modelAircraftType'=>$modelAircraftType,
		'modelAircraftSn'=>$modelAircraftSn,
		'modelProjectApproval'=>$modelProjectApproval,
	));
}

Can you help me with this. Thank you in advanced.


(fb) #2

Anyone can help me with this problem…?