【发布时间】:2021-12-08 22:02:21
【问题描述】:
伙计们。
我需要一些帮助。我正在尝试将 jQuery 自动完成添加到 PHP 表单中。我有自动完成设置,并且使用数组它工作得很好。但是,当我尝试将 PHP 模型用于 postgreSQL 数据库时,它不起作用。
模型本身是用 PHP 和 Yii1.1 框架编写的。 “Tag.php”看起来像这样:
<?php
/**
* This is the model class for table "tag".
*
* The followings are the available columns in table 'tag':
* @property string $idtag
* @property string $tag
*
* The followings are the available model relations:
* @property Dashboard[] $dashboards
*/
class Tag extends CActiveRecord
{
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tag';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('tag', 'required', 'message'=>'Please enter a tag'),
array('tag', 'type', 'type'=>'string'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('idtag, tag', 'safe', 'on'=>'search'),
);
}
/*
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'dashboards' => array(self::MANY_MANY, 'Dashboard', 'dashboard_has_tag(fk_idtag, fk_iddashboard)'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'idtag' => Yii::t('idtag', 'Tag ID'),
'tag' => Yii::t('tag', 'Tag name'),
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('idtag',$this->idtag,true);
$criteria->compare('LOWER(tag)',strtolower($this->tag),true);
return new CActiveDataProvider($this, array('criteria'=>$criteria));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Tag the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
以下代码是我尝试实际设置 jQuery 自动完成的方式:
<script type="text/javascript">
$(function() {
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
// When selecting an item with the tab key, it wont move away from the text area.
$("#tag_text").on("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
}).autocomplete({
source: function(request, response) {
$.getJSON("Tag.php", {
term: extractLast(request.term)}, response);
},
search: function() {
// Custom min Length for the Tags to be searched.
var term = extractLast(this.value);
if (term.length < 2) {
return false;
}
},
focus: function() {
// Prevents the insertion of a value as soon as one clicks it or selects it with the key.
return false;
},
select: function(event, ui) {
var terms = split(this.value);
// Remove the current user input.
terms.pop();
// Add the selected item.
terms.push(ui.item.value);
// Add a placeholder to get comma and space at the end.
terms.push("");
this.value = terms.join(", ");
return false;
}
});
});
</script>
正如我所说,如果我将源与普通数组一起使用,它就可以正常工作。但是,我确实需要从 postgreSQL 数据库中获取数据。如果有人能帮我解决这个问题,我将不胜感激。
编辑: 我一直试图让它工作,但直到现在它根本不起作用。至少不正确。 我根本无法让我的 JS 上班。到目前为止,PHP 部分似乎工作正常,但是 JS 不会在将进行用户输入的 textarea 字段中显示任何数据。 另一件事是我并没有真正得到响应数据。下面是调用PHP函数的url。
在这里我们看到我没有任何响应数据。但是,我真的不知道为什么会这样。
【问题讨论】:
标签: javascript php jquery postgresql