Welcome Guest, Not a member yet? Register   Sign In
ajax autocomplete
#1

[eluser]outtolunch[/eluser]
Hello evyerone,

Anyone tried implementing this-> http://codeigniter.com/wiki/Ajax_Autocomplete/

I can't get this thing to work. Could anyone give me a hand?

Regards.
#2

[eluser]n0xie[/eluser]
Could you show us some code? What did you try? What works? What doesn't work? Sorry my crystal ball is broken today...
#3

[eluser]outtolunch[/eluser]
I loaded up the Javascript files into my view called header.php:
Code:
[removed][removed]
[removed][removed]

My autocomplete.js file looks like that:

Code:
function lookup(inputString) {
        var inputString = document.getElementById("id_input").value;
        if(inputString.length == 0) {
            $('#suggestions').hide();
        } else {
            $.post("http://buildersuk.local/main/autocomplete/", {queryString: ""+inputString+""}, function(data){
                if(data.length >0) {
                    $('#suggestions').show();
                    $('#autoSuggestionsList').html(data);
                }
            });
        }
    }
    
    function fill(thisValue) {
        $('#id_input').val(thisValue);
        setTimeout("$('#suggestions').hide();", 200);
    }

Controller:

Code:
function autocomplete()
        {    
            $query= $this->Data->get_autocomplete($this->input->post('name'));

            foreach($query->result() as $row):

            echo "<li>".$row->name."</li>";

            endforeach;    
        }

Model:

Code:
function get_autocomplete($input){
    $sql = "SELECT * FROM trades WHERE name LIKE '".$input."%'";
    $run_sql = $this->db->query($sql);
    return $run_sql;
}

HTML

Code:
&lt;form id="search" name="search" action="" method="post"&gt;
        &lt;input name="name" id="id_input" type="text"&gt;
        <div id="suggestions">
            <div id="autoSuggestionsList">    
            </div>
        </div>
&lt;/form&gt;


So yeah, I managed to make it work, but when you type in anything it returns all the results, which i'm not happy about.
#4

[eluser]InsiteFX[/eluser]
@n0xie,

Quote:Could you show us some code? What did you try? What works? What doesn’t work? Sorry my crystal ball is broken today…

Gee I always thought that you could see the FUTURE LOL.

InsiteFX
#5

[eluser]Victor Michnowicz[/eluser]
I think the problem is that in your JS you are naming your POST variable "queryString" and in your PHP it is "name." Try changing this:

$query= $this->Data->get_autocomplete($this->input->post('name'));

to this:

$query= $this->Data->get_autocomplete($this->input->post('queryString'));




Theme © iAndrew 2016 - Forum software by © MyBB