• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Controller function not finding $_GET variable for jQuery Autocomplete

#1
[eluser]Fielder[/eluser]
I've been hacking away at this all day and searching manuals and forums... still stuck (although I've found a work-around by NOT using a controller/function url).
*Consider all javascripts properly loaded and view setup correctly*

My jQuery is
Code:
$("#storeAutoSuggest").autocomplete("/rtui/codeigniter/index.php/storeform/autocompletestore");

The jQuery plugin documents
Quote:For remote data: When the user starts typing, a request is send to the specified backend ("my_autocomplete_backend.php"), with a GET parameter named q that contains the current value of the input box and a parameter "limit" with the value specified for the max option.

A value of "foo" would result in this request url: my_autocomplete_backend.php?q=foo&limit=10

So in my storeform/autocompletestore controller, I have
Code:
function AutoCompleteStore()
    {

//TODO:: does not function with jquery autocomplete. When loading storeform/autocompletestore it does not see the "q" variable in the url
        $q = strtolower($_GET['q']);
        if (!$q) return;
        $items = array(
        "Green Heron"=>"Butorides virescens",
        "Solitary Sandpiper"=>"Tringa solitaria",
        "Heuglin's Gull"=>"Larus heuglini"
        );
        
        foreach ($items as $key=>$value) {
            if (strpos(strtolower($key), $q) !== false) {
                echo "$key|$value\n";
            }
        }

    }

I get "ERROR undefined index: q".

I know the jQuery and my form view works, because instead of loading the controller/function as I want above, if I simply load an external .php
<like this>
Code:
$("#storeAutoSuggest").autocomplete("/rtui/codeigniter/system/application/views/include/search.php");
with the exact same code in my function - it DOES work.

But I want to be able to manage the array, data, and database query calls from inside my controller.
Any thoughts?

#2
[eluser]umefarooq[/eluser]
hi you can read about get in the guide

http://ellislab.com/codeigniter/user-gui...urity.html

if you want to get the variable use
Code:
this->input->get_post('q');

This function will search through both the post and get streams for data, looking first in post, and then in get:

http://ellislab.com/codeigniter/user-gui...input.html

#3
[eluser]TheFuzzy0ne[/eluser]
As far as I know, the $_GET array is destroyed, though, unless you enable query strings from within your config.php.

#4
[eluser]Dam1an[/eluser]
If you really desperatly need to use $_GET, you could hack (extend?) the core
$_GET is unset in the _sanitize_globals() function in the input class (line 90)
You can change the protected array to stop $_GET being unset, but it may have unexpected consequences, but it will stop you from having to change any jQuery code, as it will now have access the $_GET

Disclaimer: I take no responsibility for the above and its implications

#5
[eluser]Fielder[/eluser]
I tried $autoInput = $this->input->get('q',TRUE); and then ran $autoInput through the code. Actually I tried
Code:
echo "foo";
echo $autoInput;
echo "bar";
to see if it would print the data in the variable, but I only got foo and bar, but nothing in between.

So with that said, I presume it goes back to 'enable_query_strings' set to FALSE. I was hoping to leave that set to FALSE for the security reasons I read in http://ellislab.com/codeigniter/user-gui...input.html. Do you guys think there is a way to get the q value and leave my config set to FALSE?

I did set 'enable_query_strings' to TRUE and it worked successfully. But like I mention, if that's not supposed to be set to TRUE for security purposes I'd rather not. I don't know much about the security features so leaving CI's security intact would be the best for me.

Dam1an, you totally lost me and I wouldnt even know where to begin hacking into CI's core code. I'd end up effin' something up.

#6
[eluser]Fielder[/eluser]
Here's the code I came up with that works, while setting the 'query_strings' config file to TRUE.
Code:
$autoInput = $this->input->get('q',TRUE);

$autoInput = strtolower($autoInput);
if (!$autoInput) return;
        
$storeArray = $this->Stores->storeNamesOnly();

foreach ($storeArray as $row) {
    if (strpos(strtolower($row['storename_name']), $autoInput) !== false)
    {
        echo $row['storename_name']."\n";
    }
}

Does this look right in your opinion, or a better/simpler way?

Can you guys explain how this line of code functions?
Code:
foreach ($items as $key=>$value) {
    if (strpos(strtolower($key), $q) !== false) {
        echo "$key|$value\n";
    }
}
I'm not following the $key|$value\n inside the echo quotes. Shouldn't $key be OUTSIDE the quotes? And what is the pipe for?

#7
[eluser]TheFuzzy0ne[/eluser]
It would really help if you told us where that code actually was...

#8
[eluser]Fielder[/eluser]
Code:
&lt;?php

        $q = strtolower($_GET['q']);
        if (!$q) return;
        $items = array(
        "Green Heron"=>"Butorides virescens",
        "Solitary Sandpiper"=>"Tringa solitaria",
        "Heuglin's Gull"=>"Larus heuglini"
        );
        
        foreach ($items as $key=>$value) {
            if (strpos(strtolower($key), $q) !== false) {
                echo "$key|$value\n";
            }
        }
?&gt;
which is in search.php. Search.php is called by my jQuery Autocomplete. When the user begins typing in a text field, it runs the search.php file to display the matching text. I know strpos finds the first occurrence of the string, but the "$key|$value\n" ?? In action, it's only echoing the $key information.

#9
[eluser]TheFuzzy0ne[/eluser]
Yes. I can only assume that that's the format that jQuery expects the return string to be. What were you expecting?

#10
[eluser]Fielder[/eluser]
I think I understand what you are saying, although $ is inside the quotes.... as you said that's probably what jquery is expecting so when it outputs it processes the $XXX variable. I'll see if I can decode some of the jquery plugin.
I dont know much about javascript and jquery but maybe I can find something in the sea of code.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.