Cjuiautocomplete

Hi guys good day, I want to ask something. I have here a autocomplete textfield where I type the employee id but when i save it using actionCreate it always insert an integer 1 value. This is my codes I hope someone can help me with this.

public function actionCreate()

{


	$model=new ShisConsultation;





	$timezone = "Asia/Manila";


	if(function_exists('date_default_timezone_set'))


	{


		date_default_timezone_set($timezone);


		$date = date('Y-m-d h:i:s A');


		$date2 = date('Y-m-d');


	}


	


	$row = Yii::app()->db->createCommand(array(


    'select' => array('*'),


    'from' => 'shis_duty',


    'where' => 'duty_date=:date2',


    'params' => array(':date2'=>$date2),


	))->queryRow();


	$dr = $row['duty_fullname'];


	$dr_re = $row['duty_reliever'];





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





	if (isset($_POST['ShisConsultation']['cons_type']))


	{ 		


		if($_POST['ShisConsultation']['cons_type'] == 1)


		{	


			if(isset($_POST['ShisConsultation']))


			{


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


				$model->cons_user_id = Yii::app()->user->getState('userid');


				$model->cons_date_time = $date;


				$model->cons_comment = $_POST['ShisConsultation']['cons_comment'];


				$model->cons_type = "Guest";


				if($dr_re == null)


				{


					$model->fk_duty = $dr;


				}


				else


				{


					$model->fk_duty = $dr_re;


				}


				if($model->save())


					$this->redirect(array('view','id'=>$model->cons_id));	


			}


		}


		else


		{


			if(isset($_POST['ShisConsultation']))


			{


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


				$model->cons_user_id = Yii::app()->user->getState('userid');


				$model->cons_date_time = $date;


				$model->cons_cm_id = isset($_POST['cons_cm_id']);


				$model->cons_comment = $_POST['ShisConsultation']['cons_comment'];


				$model->cons_type = "Cast Member";


				if($dr_re == null)


				{


					$model->fk_duty = $dr;


				}


				else


				{


					$model->fk_duty = $dr_re;


				}


				if($model->save())


					$this->redirect(array('view','id'=>$model->cons_id));	


			}


		}


	}


	





	$this->render('create',array(


		'model'=>$model,


	));


}

this is my form

<?php

				&#036;form-&gt;widget('zii.widgets.jui.CJuiAutoComplete', array(


					'name'=&gt;'cons_cm_id',


					'source'=&gt;&#036;this-&gt;createUrl('shisConsultation/autocompleteTest'),


					'options'=&gt;array(


							'showAnim'=&gt;'fold',


					),


				));


			?&gt;

and this is my autocompleteAction

public function actionAutocompleteTest() {

&#036;res =array();


if (isset(&#036;_GET['term'])) {


    // http://www.yiiframework.com/doc/guide/database.dao


    &#036;qtxt =&quot;SELECT cm_id FROM shis_cm WHERE cm_id LIKE :cm_id&quot;;


    &#036;command =Yii::app()-&gt;db-&gt;createCommand(&#036;qtxt);


    &#036;command-&gt;bindValue(&quot;:cm_id&quot;, &#036;_GET['term'].'%', PDO::PARAM_INT);


    &#036;res =&#036;command-&gt;queryColumn();


}


echo CJSON::encode(&#036;res);


Yii::app()-&gt;end();


}

Tip: Try to use code snippet on your code, it will help to read and understand your code.




public function actionAutocompleteTest() {

$res =array();

if (isset($_GET['term'])) {

// http://www.yiiframew...de/database.dao

$qtxt ="SELECT cm_id FROM shis_cm WHERE cm_id LIKE :cm_id";

$command =Yii::app()->db->createCommand($qtxt);

$command->bindValue(":cm_id", $_GET['term'].'%', PDO::PARAM_INT);

$res =$command->queryColumn();

}

echo CJSON::encode($res);

Yii::app()->end();

}




Before you do CJSON::encode($res), what’s result of $res you got?

tnx for the reply well, I already fixed this. In my form i just add the model in my cjuiautocomplete and it works like magic. But for my next question. Do you know how to fill the other textfield after using autocomplete? because after loading the employee id i want to autofill the employee name and department. tnx again

I don’t have available code, but below is one example, which depends one Ajax call return data to fill two fields.




success:function(engdata){engineer=jQuery.parseJSON(engdata);

$("#ToolRequestPage1_engineer_name").val(engineer.engname);

$("#ToolRequestPage1_engineer_email").val(engineer.engemail);

}



Also search ‘ajax’ and see other examples in the forum.

tnx man :)