0

Jquery's autocomplete documentation seems to be a bit lacking, at least for beginners. I've scoured the net for good tutorials, but haven't found any.

Heres what I have so far:

HTML:

<div id="addfilter">
    <div id="textwrap">
    <form method="POST" class="homeForm" id="homeForm" action="<?php echo base_url() ?>main/add_filter">
        <input type="text" class="text" id="homeText" name="homeText" placeholder="Add Category"></input>
        <input type="image" class="imginput" src="<?php echo base_url() ?>img/board/icons/add.jpg"id="homeSubmit" value="X"></input>
    </form>
    </div>
</div>

JS:

$().ready(function() {
    $("text").autocomplete("search.php", {
        width: 260,
        matchContains: true,
        selectFirst: false
    });
});

search controller

    public function search()
    {
        $this->thread_model->autocomplete();
    }

autocomplete model

    public function autocomplete()
    {
        $query = $this->db->query('SELECT tag
            , COUNT(*) as num_items
            FROM filter_thread ft
            INNER JOIN filter f
            ON ft.filter_id = f.filter_id
            GROUP BY tag');
        $tagcloud = $query->result_array();
        foreach ($tagcloud as $tags)
        {
            echo $tags;
        }
    }

Firebug doesn't show anything happening when I input text into 'formText.' I'm not even sure if the output of the model is in the correct format. Any help would be appreciated!

5
  • Just a question does $().ready(function() { works without mentioning document in braces? Commented May 29, 2012 at 14:02
  • where do you filter your table? you didn't use the input as a filter here? Commented May 29, 2012 at 14:16
  • I returned it in JSON...again, not sure what the correct format should be Commented May 29, 2012 at 14:18
  • By filter table, do you mean create a div/table to display the responses? I'm not sure Commented May 29, 2012 at 14:19
  • No, I meant the LIKE("%'.$this->db->escape($_POST['term']).'%") part thatin the answer @liaant wrote. Commented May 30, 2012 at 7:57

1 Answer 1

2

Have you tested your controller and model?

First of all I'd correct your jquery:

$(document).ready(function() {
        var url = $("#homeForm").attr("url");
        $("#homeText").autocomplete({
            source: url,
            width: 260,
            matchContains: true,
            selectFirst: false
        });
    });

I'd recomment to supply URL the other way like attribute in form tag:

<form url="<?php echo base_url() ?>main/search" .... >

And before autocomplete in jQuery get that URL:

var url = $("#homeForm").attr("url")

Autocomplete then sends constraint by in term parameter. You shoud adjust your query to something like that:

'SELECT tag, COUNT(*) as num_items
            FROM filter_thread ft
            INNER JOIN filter f
            ON ft.filter_id = f.filter_id
            WHERE <YOUR_FIELD_HERE> LIKE("%'.$this->db->escape($_POST['term']).'%")
            GROUP BY tag'

I'm not really familiar with CodeIgniter, so you maybe need to correct something.

And the last (maybe) thing is to see how autocomplete will react with returns.

Sign up to request clarification or add additional context in comments.

5 Comments

here is a link to my model: rickymason.net/blurb/main/search . It seems to work, and seems to output what is echoed. It is currently not in JSON, not sure if that matters.
I updated based on your suggestions. It appears to get some input lag when typing text in the text field, though I still don't see anything on firebug (not sure if I should). What format should the model return the text?
@RickyMason do you see anything in Network tab in Firebug? It should be sending XHR requests if anything alright. The second matter is what you're receiving and how autocomplete should intrepere it
I am seeing nothing. For reference: you can see the page here: rickymason.net/blurb/main/home
@RickyMason updated my answer. Fixed url as it was incorrect in action and jquery autocmplete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.