Detection of form refresh and callback! How is it possible to detect if the page, in which the form is, is refreshed and to detect if only the form is refreshed?

I tell you the whole thing: I'm building a module. The form in this module has ajax-ed elements. When the form is first loaded (or when the page containing the form is refreshed) a database query is executed (filling up the form), that is the thing i want. But when an ajax-ed element is triggered the query is executed again, and i don't want that.

How is it possible to stop the execution of this query when an ajax callback is executed?
The form is in an .inc file and called by the .module file via drupal_get_form().
You can use $form_state['triggering_element'] to find out if and by which element a form was submitted.

That said, I am not sure if what you want to do is possible. When rebuilding the form, you will have to provide all valid form elements and options for them again.

If that query is slow, you might be able to use the caching system instead (That is, unless these options are personalized for the current user).

But maybe I didn't exactly get what you meant, so try it..