cjuiautocomplete ajax,php - Autocomplete other field with Yii CJuiAutoComplete - Stack Overflow

I've got the following situation:

Form with a field for postal codes, and a field for cities.

I want an autocomplete on the postalcode field, so when the user types for instance 1000, the autocomplete values will show "1000 - Brussels". When this value is then selected, 1000 gets filled in in the postal code field, and Brussels gets filled in in the City field.

Postal code, city, and concatenated info will come from a mysql database:

I have the autocomplete working with only the postalcodes, but have no clue on how to implement the described effect (= populating a second field).

Current form code:

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

<?php //echo $form->textField($model,'PostalCode',array('size'=>10,'maxlength'=>50));

$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

'name'=>'PostalCode',

'value'=>$model->PostalCode,

//'source'=>$people, //

'source'=>$this->createUrl('BeCity/GetBelgianPostalCodes'),//

// additional javascript options for the autocomplete plugin

'options'=>array(

'minLength'=>'1', // min chars to start search

'showAnim'=>'fold'

),

));

?>

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

Current controller action code:

public function actionGetBelgianPostalCodes()

{

$res =array();

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

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

$qtxt ="SELECT

DISTINCT

bc.PostalCode as PostalCode,

bc.NameNL as CityName,

CONCAT(bc.PostalCode, ' - ', bc.NameNL) as FullCityName

FROM be_city bc

WHERE bc.PostalCode LIKE :qterm

ORDER BY bc.PostalCode, bc.NameNL ASC";

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

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

$res =$command->queryColumn();

}

echo CJSON::encode($res);

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

}

Not sure if the controller action is entirely correct, the $command->queryAll() doesn't seem to work so I use queryColumn() instead but it returns only the first column ?

Any hints ?

Also, additional question, I'd like the link to the controller action to be dynamic. If the in a previous Country dropdown the user selected Belgium then the call should be made to 'BeCity/GetBelgianPostalCodes'. If it was France, it should be to 'FrCity/GetFrenchPostalCodes'. Is this possible, and how ?

thanks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值