Click to See Complete Forum and Search --> : [RESOLVED] auto-suggest JS that works?


NogDog
09-22-2009, 01:01 PM
I want to include an auto-suggest/auto-complete feature for a couple text input fields in a form. I've tried installing and running a number: a couple based on jQuery, one on Scriptacuolus, and one for MooTools, and all have failed to work (most doing nothing but generating JS notices, one coming up with a list for the first character(s) typed, then doing nothing if you type more characters or change what you typed.

Have any of you found an auto-suggest script that (a) works, (b) does not require a PhD in JavaScript to get it to work, (c) works, (d) is straight-forward to install and well documented, and (e) works?

(I would prefer one that sends the request to the server via POST, as that is easier for me to deal with on the PHP side of things, but I'll deal with GET requests if necessary.)

Thanks.

NogDog
09-22-2009, 03:50 PM
I finally found one that looks like it works: http://www.brandspankingnew.net/archive/2007/02/ajax_auto_suggest_v2.html.

I had to make one small tweak in the javascript so that I could use it with CodeIgniter URLs:

// create ajax request
//
if (typeof(this.oP.script) == "function")
var url = this.oP.script(encodeURIComponent(this.sInp));
else
// var url = this.oP.script+this.oP.varname+"="+encodeURIComponent(this.sInp);
var url = this.oP.script+encodeURIComponent(this.sInp);

Then in the JS on the page where I start things up, I can use the "friendly" URL as:

<script type='text/javascript'>
// <![CDATA[
var options = {
script:"/index.php/ajax/author_auto_complete/",
json:true
};
var as_json = new bsn.AutoSuggest('author', options);

// ]]>
</script>

Now I can just go play with the CSS to make it fit with the rest of the page. :)

Shrike
09-28-2009, 10:09 AM
For ease of use, it's hard to top jQuery's autocomplete plugin:

$(formfield).autocomplete( 'http://www.myajaxsite.php' ) // PHP returns a line-separated result set, the plugin does the rest