By Vic96 on
In a signup form that was created as a node, I'm asking people to select their favorite movie. For consistency, instead of giving them a free text field, I want them to search for the movie name from a table and then select the option from the results that are returned. What is the best way to handle this? There will be tens of thousands of records. Alternatively, is there any way to help them autocomplete the field or populate the drop down from the database. All the current solutions in modules can't seem to handle tens of thousands of records.
I would appreciate any guidance.
Thanks,
Vic
Comments
Re: Search database and add value to field in signup form
Hi Vic:
Perhaps you might want to use the #autocomplete_path attribute of the textfield in question?
(please note that this code is typed directly into the comment, untested :)
Something like this:
$form['my_fav_movie'] = array(
'#type' => 'textfield',
'#title' => t('My favourite movie is'),
'#autocomplete_path' => 'mymodule/autocomplete/fav_movie'
);
Then you need to create an item in your hook_menu implementation bound to the path mymodule/autocomplete/fav_movie that returns a set of matches based on whatever they've typed in. You could "enforce" the match by ensuring that the user has submitted one of your autocompleted answers in the validation stage.
I generally code up the autocomplete callback to return nothing until I have two (or three, or four - depending on the application) characters. SELECT title FROM {movies} WHERE title like 'a%' is going to return a LOT of entries - but limiting it to a slightly longer string keeps the list more manageable.
Here's a link to the relevant piece of the forms api reference:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
I hope this has been helpful.
Cheers,
Steve
Very helpful. I'll try this.
Very helpful. I'll try this. Thank you, Steve!
Oh - almost forgot..
your callback should dump out something like this:
$results = array( 'foo' => 'Matching foo!', 'bar' => 'Matching Bar!', 'baz' => 'Matching baz!' );
print drupal_to_js( $results );
You are welcome - good luck!
Here's the code I'm using but
Here's the code I'm using but it doesn't seem to work.
I'm trying to get it to offer movie options based on the few characters the person types. The user can then choose one as his/her favorite movie. I'd appreciate some guidance. Thanks!