Use case: Search field e.g. in the header, that leads to a url with GET query string.

The usual flow of forms built with Drupal 7 form API is like this:

  • User visits a page with the form.
  • User submits, $_POST or $_GET
  • Validate + submit handlers are called
  • Redirect to a new page.

What i would like instead:

  • User visits page with the form, that was built with form API.
  • User submits, resulting in a $_GET request, e.g. example.com/search?query=foo+bar.
  • The page example.com/search?query=foo+bar is served, without thinking about form API.

So, I want to use form API to build the form HTML (cause it's convenient), but not any submit magic.

Maybe I could find an example in existing modules, but I am looking for a "big picture" or documentation.

  • Why not make a submit button that just does a drupal_goto, with your query ? – Gregory Kapustin Oct 31 '13 at 17:05
  • sure this is an option. But it is a wasted http request with Drupal bootstrap. – donquixote Oct 31 '13 at 17:10
  • You mean the destination URL is always the form URL ? – Gregory Kapustin Oct 31 '13 at 17:15
  • destination url is the form action attribute. – donquixote Oct 31 '13 at 17:17

Not sure what you're exactly asking but everything should be well documented on Form API

I've recently done a form like this so I can give you an example of a GET method form:

 * Form function for iwm_collections_search_form block
function mymodule_search_form($form, &$form_state) {
  $form['#action'] = url(MYMODULE_SEARCH_PATH);
  $form['#method'] = 'get';
  $form['#attributes'] = array('class' => array('form-main'));
  $form['#after_build'][] = 'mymodule_search_form_after_build';
  $form['query'] = array(
    '#type' => 'textfield',
    '#title' => t('Keywords'),
    '#default_value' => '',
    '#size' => 30,

  $form['submit'] = array(
    '#type' => 'submit',
    '#name' => '',
    '#value' => '',
    '#attributes' => array(
      'class' => array(

  return $form;

You need an #after_build function to remove the unnecessary hidden fields so that it doesn't clutter the address bar:

 * After build callback for mymodule_search_form
function mymodule_search_form_after_build($form) {
  return $form;

No javascript, no submit handlers, easy.

|improve this answer|||||
  • Interesting. I suppose the form still goes into the $_SESSION, does it? Not a big problem, but it feels wrong.. (not saying this is your fault, maybe this is the only way) – donquixote Oct 31 '13 at 17:14
  • You drink the water... you follow the rules :) If you want to use Form API then you gotta accept the rules – Beebee Oct 31 '13 at 17:16
  • Ok so this means there is no API in Drupal that is "designed" for this kind of thing. – donquixote Oct 31 '13 at 17:19
  • I think it's designed this way to keep it secure from various injections and tampering with the form. Not 100% sure on that. – Beebee Oct 31 '13 at 17:20

If you don't want to build a submit function that simply does a drupal_goto, you can set a dummy submit button and with Javascript, preventDefault its action, and then do whatever you want as a query.

|improve this answer|||||
  • This sounds more like a workaround, and requires javascript. Not a bad answer, but there might be better ways. – donquixote Oct 31 '13 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.