Welcome Guest, Not a member yet? Register   Sign In
can't get autocomplete value
#1

[eluser]titolancreo[/eluser]
Hi!

I want to have an autocomplete input and I choose jquery.

I have two problems:
1. On my controller, i try to get term value but I always get 0.
2. If I recieve elements, I can`t see anything, but If I click, it works.

I have not CSRF

This is my javascript
Code:
[removed]
        $(document).ready(function()
        {

          $( "#autocomplete" ).autocomplete({
            source: function( request, response ) {
              $.ajax({
                url: "autocomplete/buscar",
                data: { term: $("#autocomplete").val()},
                dataType: "json",
                success: function( data )
                {
                   response(data)
                }
              });
            },
            minLength: 2,
            select: function( event, ui ) {
              alert( ui.item ?
                "Selected: " + ui.item.nombre :
                "Nothing selected, input was " + this.value);
            }
          });
        });
  [removed]

This is my controller. if I'm debbuging, I can see that "$term " is boolean and 0, and I need the text writen by the user.
Code:
if($this->session->userdata('logged_in'))
        {
            $term = $this->input->post('term');
            //if($buscar)
            //{
                $list=$this->DaoAutocomplete->autocompleteUsers($term );
                $i=0;
                foreach($list as $row)
                {
                    $data[]= $row; //each row has id and name
                }
                echo json_encode($data);
            //}
        }


So, how can I have in $term the string? How can I see all names in autocomplete (See photo)

Thanks!!!
#2

[eluser]TheFuzzy0ne[/eluser]
What is your response() function? By default, jQuery uses GET to query the server. If you're sending data, you should use POST.

If you have Firefox, have you tried the Firebug extension? It's very handy for debugging problems like this.
#3

[eluser]titolancreo[/eluser]
I don't know what else I need to do, I only have what you see. I thought that response function was on jquery code, but because of your ansewer, maybe I have to write it...

I know that it uses get, but I want to do it with post, and that's not my problem, because it works (more and less)
if I send "as" for example, I can't get it, but I get first 10 rows from my database instead of the 10 rows that contains "as".
At the endo of controller I send it by echo json_encode and it works, because I can see them with Firebug and also, with alerts, but I don't know why, I can't see this 10 names in the dropdown menu.

So, I want to know why in my controller, at this line <code>$term = $this->input->post('term');</code> I get $term as a boolean with value 0 instead of a string with value "as"
#4

[eluser]TheFuzzy0ne[/eluser]
Have you checked that the correct term is being sent to the server?

Also, does changing this:
Code:
data: { term: $("#autocomplete").val()},

to this:
Code:
data: { term: $(this).val()},

make any difference?
#5

[eluser]titolancreo[/eluser]
I think my error is sending because I use the same ajax function to send other things, but without autocomplete.

If I change it to $(this).val, there is an error, because jquery cannot call method 'toLowerCase' of undefined.

I changed
<code>$buscar = $this->input->post('term');</code>
to
<code> $buscar = $this->input->get('term');</code>
and now it works, but why? I have enable_query_strings= FALSE

Maybe should I change my controller?
Code:
$list = $this->DaoAutocomplete->autocomplete($term); //it returns 10 names
                $i=0;
                foreach($lista as $row)
                {
                    $data[]= $row; //each row contains only one name ($row->name)
                }
                echo json_encode($data);
#6

[eluser]TheFuzzy0ne[/eluser]
If you're using GET, you may run into problems with characters that aren't allowed in the URL, so I'd recommend using POST instead.

Also, it would keep your controller cleaner if you had your model return the JSON encoded string, or even output it directly.

I agree. Your model/controller should only be echoing a JSON array of strings only, not IDs.
#7

[eluser]titolancreo[/eluser]
I couldn't do it with autocomplete... so I did myself with a normal ajax call.
#8

[eluser]TheFuzzy0ne[/eluser]
If you get stuck, try comparing your code to the example on the Web site. http://jqueryui.com/autocomplete/
#9

[eluser]titolancreo[/eluser]
That was the first thing I did. I also tried copying it but it doesn't work.

Anyway, it doesn't matter, I can't spend more time on it, I did it myself.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB