使用jquery和codeigniter php MVC自动完成

问题描述:

I'm trying to build an autocomplete using jquery plugin autocomplete from this site.

Now I managed to achieve autocomplete using local results pre-loaded into website as a part of document ready function, its quite easy doing it localy.

Here is with what I struggle with, pulling results from php file. Here is how I tried:

$("#post_tags").autocomplete("http://localhost/tags/filter", {
    width: 260,
    selectFirst: false,
    highlight: false,
    multiple: true,
    multipleSeparator: " ",
    scroll: true,
    formatItem: function (row, position, totalReturned, searchTerm){
      return row[0];
    },
    formatResult: function(row, position, totalReturned){
      return row[0].replace(/(<.+?>)/gi, '');
    }
 }).result(function(event, data, formatted){
    $("<li>").html( !data ? "No match!" : "Selected: " + formatted)
     .appendTo("#result");
 });

When I manually go to http://localhost/tags/filter/p I get results php,asp because they contain letter p .

How can I make this work with autocomplete, I mean its quite easy using native php see this file , meaning I'm only passing something to the search file like this search?q=p and I get back the results. Since I'm using codeigniter the things are a bit different and I don't retrieve any results using jquery code above.

I'm stuck with this since last night I cannot yet offer a bounty and I really need it for monday. Thank you

我正在尝试使用来自这个网站。 p>

现在我设法使用预先加载到网站中的本地结果作为文档就绪函数的一部分实现自动完成,它非常容易在本地进行。 p>

这是我努力的结果,从php文件中提取结果。 以下是我尝试的方法: p>

  $(“#post_tags”)。autocomplete(“http:// localhost / tags / filter”,{
 width:260,\  n selectFirst:false,
 highlight:false,
 multiple:true,
 multipleSeparator:“”,
 scroll:true,
 formatItem:function(row,position,totalReturned,searchTerm){
 return row [  0]; 
},
 formatResult:function(row,position,totalReturned){
 return row [0] .replace(/(&lt;。+?&gt;)/ gi,''); 
}  
})。结果(函数(事件,数据,格式化){
 $(“&lt; li&gt;”)。html(!data?“No match!”:“Selected:”+ formatted)
 .appendTo  (“#result”); 
}); 
  code>  pre> 
 
 

当我手动转到 http:// localhost / tags / filter / p 代码>我得到结果php,asp因为它们包含字母p。 p>

如何使用自动完成工作,我的意思是使用原生php很容易看到这个文件,这意味着我只是将一些东西传递给搜索文件,比如 search?q = p code>然后我回来了 结果。 因为我使用的是codeigniter,所以事情有点不同,我不会使用上面的jquery代码检索任何结果。 p>

自从昨晚我还不能提供 赏金,我真的需要星期一。 谢谢 p> div>

In your jquery.autocomplete.js file, at around line 360 (as of the latest version), add one line to make the section look like this:

$.ajax({ 
    type: "post", // This is the new line
    // try to leverage ajaxQueue plugin to abort previous requests
    mode: "abort",
    // limit abortion to this input
    port: "autocomplete" + input.name,

(If you are using a minified version of the autocomplete plugin, just do a search for .ajax and insert the line as appropriate).

This change causes the autocomplete script will now send form data by POST, rather then GET. Accessing this from within codeigniter can be done as follows:

public function filter()
{
    $filter_by = $this->input->post('q');
}

And from this you can generate your list as required. I'm not sure if there is a nicer way to do this (one which doesn't require you to edit the source code), but it works for me.