I have made a module in Drupal 6 that builds a page that shows some data taken from an external database. I want to add a form that let the user filter the data (for example, limit the data output to a specific city instead all the cities). How can I get the value of the submitted form and use it as a filter condition on the page rendering function? The $form_state variable doe not exist when running the page function and I have no way to load the value.
2 Answers
It sounds like your problem is about passing the selected value to your page rendering function, upon form submission. There are a few different ways to pass that value from within the form_submit function, including via a URL argument, or setting a cookie, or using some javascript to post the value - or even using variable_set() in the right circumstances. There might be easier better ways than those, and I'd like to know what they are if they exist.
Here's a way to do it via a URL argument:
In the form_submit() function, pass the select data to the page rendering function via drupal_goto(). Eg: drupal_goto( 'this/page/' . $form_state['values']['city']
Be sure that your page rendering function can accept the right argument. in your hook_menu:
'page arguments' => array( 2 ),
(or you can just grab the argument using arg( 2 ) from within the page rendering function)
I'm not saying this is necessarily the best way to get the job done, but this should work.
You could make the external database table visible to Views by using hook_views_data(). Rather than add in a form, you could then leverage the power of Views and have exposed filters.