我想要两个具有相同格式的自动填充字段, 第二个字段应取决于第一个字段的值。
我试图这样做:
视图:
<div class="form-group">
<?php echo $form->labelEx($model, 'cityId', ['class'=> 'col-xs-4 control-label']); ?>
<div class="col-xs-8">
<?php $this->widget('CAutoComplete',
array(
'name'=>'city_name',
'url'=>array('/site/cityLookup'),
'max'=>10,
'minChars'=>2,
'delay'=>500, //number of milliseconds before lookup occurs
'matchCase'=>false, //match case when performing a lookup?
'htmlOptions'=>array('size'=>'25', 'class'=>'form-control'),
'methodChain'=>".result(function(event,item){\$(\"#city_id\").val(item[1]);})",
)
);
?>
</div>
<?php echo CHtml::hiddenField('city_id'); ?>
<?php echo $form->error($model,'cityId'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'streetId', ['class'=> 'col-xs-4 control-label']); ?>
<div class="col-xs-8">
<?php $this->widget('CAutoComplete',
array(
'name'=>'street_name',
'url'=>array('/site/streetLookup'),
'max'=>10,
'minChars'=>2,
'delay'=>50,
'matchCase'=>false,
'htmlOptions'=>array('size'=>'25', 'class'=>'form-control'),
'methodChain'=>".result(function(event,item){\$(\"#street_id\").val(item[1]);})",
));
?>
<?php echo CHtml::hiddenField('street_id'); ?>
<?php echo $form->error($model,'streetId'); ?>
</div>
</div>
第一个现场控制器操作:
public function actionCityLookup() {
if(Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
$name = isset($_GET['q']) ? htmlspecialchars($_GET['q'], ENT_QUOTES) : '';
$arrSQL = array();
$num_results=0;
if ( $name != "" AND strlen($name) >=3 ) {
$searchArray = explode(" ", $name);
$num_params=0;
foreach ($searchArray as $key => $word) {
if (strlen($word) >2 ) {
$num_params++;
$po = "1.0";
$tochki = "3";
$field = "md2.name";
$arrSQL[] = "((LENGTH(uCase($field))-LENGTH(REPLACE(uCase($field),uCase('$word'),'')))/LENGTH(uCase('$word'))*(($tochki)*LENGTH(uCase('$word')))) ";
}
}
if (sizeof($arrSQL) == 0) {
$points = 0;
} else {
$points = implode("+", $arrSQL);
}
$qtxt ="SELECT ($points) as points, m.*, md.name, md.region
FROM cities2 m
LEFT JOIN cities_details2 md ON (m.id= md.elem_id AND md.lang_id = '1')
LEFT JOIN cities_details2 md2 ON (m.id= md2.elem_id )
WHERE m.post_code > 0 AND ($points) > 0
ORDER BY points DESC";
$limit = min($_GET['limit'], 50);
$criteria =Yii::app()->db->createCommand($qtxt);
$cityArray = $criteria->queryAll();
$returnVal = '';
foreach($cityArray as $city) {
$returnVal .= $city['post_code'].' '.$city['name'].' ('.$city['region'].')|'.$city['city_id']."\n";
}
echo $returnVal;
}
}
}
第二场控制者动作:
public function actionStreetLookup() {
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']) && isset($_REQUEST['city_id'])) {
$name = $_GET['q'];
$city = $_REQUEST['city_id'];
$limit = min($_GET['limit'], 50);
$qtxt ="SELECT * FROM offices2 t LEFT JOIN offices_details2 td ON (t.id= td.elem_id AND td.lang_id = '1') WHERE t.city_id = '".$city."';";
$limit = min($_GET['limit'], 50);
$criteria =Yii::app()->db->createCommand($qtxt);
$cityArray = $criteria->queryAll();
$returnVal = '';
foreach($streetsArray as $street) {
$returnVal .= $street['name'].'|'.$street['id']."\n";
}
echo $returnVal;
}
}
$ _ POST ['city_id]不起作用或无法从字段中获取数据的原因是什么?有人会建议解决我的问题吗?
$ _ POST ['city_id]无效。.
请帮助。
第二个字段应在选择菜单中显示办公室地址,但..不会。
我已经取得了进展,已经有了办公室,但是我无法选择与他们一起工作的床单。
查看文件;
<div class="form-group">
<?php echo $form->label($model, 'city_id', ['class'=> 'col-xs-4 control-label']); ?>
<div class="col-xs-8">
<?php
$this->widget('CAutoComplete',
array(
'name'=>'RegistrationForm[city_name]',
'url'=>array('/site/cityLookup'),
'max'=>20,
'minChars'=>2,
'delay'=>300, //number of milliseconds before lookup occurs
'matchCase'=>false, //match case when performing a lookup?
'htmlOptions'=>array('size'=>'25', 'class'=>'form-control'),
'methodChain'=>".result(function(event,item){\$(\"#city_id\").val(item[1]);})"
)
);
?>
</div>
<?php echo CHtml::hiddenField('city_id'); ?>
<?php echo $form->error($model,'city_id'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model, 'street_id', ['class'=> 'col-xs-4 control-label']); ?>
<div class="col-xs-8">
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'street_name',
'source'=>'js: function(request, response) {
$.ajax({
url: "'.$this->createUrl('/site/streetLookup').'",
dataType: "json",
data: {
term: request.term,
countryCode: $("#city_id").val()
}, success: function (data) {
response(data);
}
})
}',
'htmlOptions'=>array('size'=>'25', 'class'=>'form-control'),
'options'=>array(
'minLength'=>'2',
'select'=>'js:function(event, ui) { $("#street_id").val(ui.item.value); }'
),
));
?>
<?php echo CHtml::hiddenField('street_id'); ?>
<?php echo $form->error($model, 'street_id'); ?>
</div>
</div>
和控制器操作代码;
public function actionStreetLookup() {
$countryCode = isset($_GET['countryCode']) ? htmlspecialchars($_GET['countryCode'], ENT_QUOTES) : '';
$term = isset($_GET['term']) ? htmlspecialchars($_GET['term'], ENT_QUOTES) : '';
if(Yii::app()->request->isAjaxRequest && isset($_GET['countryCode'])) {
$qtxt = "SELECT * FROM offices2 t LEFT JOIN offices_details2 td ON (t.id= td.elem_id AND td.lang_id = '1') WHERE t.city_id = '".$countryCode."';";
$criteria = Yii::app()->db->createCommand($qtxt);
$streetsArray = $criteria->queryAll();
$returnVal = '';
foreach($streetsArray as $street) {
$returnVal .= $street['name'].' ('.$street['address'].') '.'|'.$street['id']."\n";
}
echo $returnVal;
}
}
我认为,如果可能的话,如果数据库中没有办公室,则可以插入一个选项来显示简单地写着“至地址”的选择菜单
问题可能出在此代码中,无法可视化已获取的信息。有人知道吗?
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'RegistrationForm[street_name]',
'source'=>'js: function(request, response) {
$.ajax({
url: "'.$this->createUrl('/site/streetLookup').'",
data: {
term: request.term,
countryCode: $("#city_id").val()
}, success: function(data) {
$(\'#RegistrationForm_street_name\').html(data);
}
})
}',
'htmlOptions'=>array('size'=>'25', 'class'=>'form-control'),
'options'=>array(
'minLength'=>'2',
'select'=>'js:function(event, ui) { $("#street_id").val(ui.item.value); }'
),
));
?>