By frdesign on
I can't get my autocomplete form field to work. It just shows up as a regular textfield. Part of the problem is that I've found examples showing different ways of doing it and I don't know which is right.
This is field is step 3 of a multistep ahah form. The dependent fields are Continent->Country->City. Continent and Country are select fields. Originally City was a select field as well and everything worked fine but now I want City to be an autocomplete textfield because some countries have way too many cities.
Here's my menu item:
$items[city/autocomplete] = array(
'title' => t('City autocomplete'),
'page callback' => 'city_autocomplete',
'access callback' => TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
Here's my form item and my callback with db query:
$form['globalize']['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
'#size' => 40,
'#autocomplete_path' => 'city/autocomplete',
);
//Pulls value from another form field to use as argument.
$countryID = $form_state['values']['country'];
function city_autocomplete($string = '') {
$matches = array();
if ($string) {
$result = db_query_range('SELECT cityID, cityName FROM {globalize_cities} WHERE countryID = "%s" AND LOWER(name) LIKE LOWER("%s%%")', $countryID, $string);
while ($row = db_fetch_object($result)) {
$matches[$row->cityID] = $row->cityName;
}
}
drupal_json($matches);
}
Thanks
Comments
A few pointers
This block of code has at least two major problems.
First it appears that you are nesting functions
So it should look something like this.
Second and what is probably causing most of your problems the $countryID variable is not available inside the city_autocomplete function. You defined it inside another function. To get it inside the city_autocomplete function I would recommend code that looks something like this.
Since I haven't actually run the code I expect there are a few bugs but that should get you on the right track.
Oh and one other pointer. I assume that you are creating a module. If so your function names are incorrect. They need to start with the moduleName_. (See coding standards http://drupal.org/node/318). Also the coder module (http://drupal.org/project/coder) is fairly straightforward and will greatly help in catching things like the function names.
Hope this helps.
Wow! Thanks for the detailed
Wow! Thanks for the detailed response. I'll get cracking on this right away and post my results.
I really appreciate it!
Major breaktrough followed by major head scratching
So after I applied the suggested code corrections I was still not getting autocomplete functionality.
First I thought the problem was in my autocomplete function. Someone in the #drupal irc channel suggested I put an esc() command in my function to see if it was being executed. I was supposed to get a white screen but the page was loading fine which I assumed meant my function wasn't being executed. I double checked callback and path names which were fine.
Next I thought maybe my $countryID value still wasn't being passed to the autocomplete function so I put the value into a $_SESSION variable so I could access it. Still no dice. So I simplified everything as much as possible. I commented out the if->then statements, created a simple test autocomplete field and voila! The test autocomplete field worked beautifully. Then I activated my original "city" field and that worked as well.
But here's where the head scratching comes in. When I deleted my test field the real "city" field inside the if->then statements stops working. I brought back the test field and they both started working again. I commented out the #autocomplete_path in the test field and the functionality disappears on both again, like they're connected somehow.
It seems the real "city" autocomplete field doesn't like if/then statements so I tried switch/case instead. Same results. Everyone I asked at #drupal is as puzzled as I am. Being that this is my first real coding attempt I'm excited I got this far and I'm hoping with some help I can get to the finish line.
Here's my module code as it stands:
http://drupalbin.com/4721
Good enough for now
I tried hiding the test field on my themes template.php using hook_theme but that broke my autocomplete functionality. I decided to just keep the test field for now and reduce it's visibility as much as possible. It's not an ideal solution but it's a minor trade off to maintain the desired functionality in the real form which is critical to my application.
For the test field I deleted the #title attribute and added the '#disabled' => TRUE attribute so it can't actually be used. I also put it below the real form and will theme it with a white border(once I figure out how to do this) which will make it almost invisible. Only the little progress throbber will be visible but since it's disabled I can live with that.
I'm wondering if this can be considered a bug with autocomplete. Should I submit an issue?