【发布时间】:2015-06-21 14:14:11
【问题描述】:
首先,是的,我知道还有其他类似的问题,我已经尝试了那里的解决方案,但没有运气。
我正在使用 PHP、codeigniter 和 jquery UI 自动完成功能。我正在关注this 教程,但它不起作用。
我的模特:
class Tags_model extends Model{
function get_tag($q){
$db = mysqli_connect("localhost","root","","km_portal") or die("Could not connect");
$query = mysqli_query($db,"SELECT * FROM resource WHERE tags LIKE '%".$q."%'");
if(mysqli_num_rows($query) > 0){
while($row = mysqli_fetch_array($query)){
$new_row['label']= $row['tags'];
$new_row['value']= $row['tags'];
$row_set[] = $new_row; //build an array
}
echo json_encode($row_set); //format the array into json
}}}
我的看法:
<?php echo form_open('search/do_search');?>
<input type="text" name="search" id="tags" placeholder="Search tags"/>
<input type="submit" value=">>" />
<span id="noMatches"></span>
</form>
使用javascript:
<script type="text/javascript">
$(jQuery(document).ready(function(){
$("#tags").autocomplete({
source: "search/get_tags",
response: function(event,ui){
if (ui.content.length === 0) {
$("#noMatches").html("No matches");
} else {
$("#noMatches").empty();
}
}
});
}));
最后,我的控制器:
function get_tags(){
$this->load->model('tags_model');
if(isset($_GET['term'])){
$q = strtolower($_GET['term']);
$this->tags_model->get_tag($q);
}}
当我尝试运行它时检查 Chrome 控制台,错误似乎是参数 'term' 将自身附加到 url 使其成为:localhost/km/index.php/search/get_tags?term=a。我收到错误 404(未找到)
任何帮助将不胜感激。我已经坚持了一个多月(这是我第一次使用codeigniter)。
谢谢!
编辑:使用以下js:
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>jquery-ui-1.11.4.custom\jquery-ui.css"/>
<script type="text/javascript" src="<?php echo base_url();?>jquery-ui-1.11.4.custom\external\jquery\jquery.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>jquery-ui-1.11.4.custom\jquery-ui.js"></script>
【问题讨论】:
-
你添加
jquery.ui.js了吗? -
您好,是的,我已经链接到它了。感谢您提醒我提及这一点。
标签: javascript php jquery codeigniter autocomplete