Hi all. I have a problem that I'm betting has been addressed many times before, but I haven't found any discussion of it.

I'm creating a page with a search form at the top:

function my_search_form($text = NULL) {
  $form = array();
  $form['keywords'] = array(
    '#type' => 'textfield',
    '#title' => t('Words'),
    '#default_value' => $text,
    '#description' => t('Enter word(s).'),
    '#size' => 40,
    '#maxlength' => 255
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#name' => 'op-submit',
    '#value' => t('Search')
  );
  return $form;
}

function my_page() {
  $output = drupal_get_form('my_search_form');

  $output .= other stuff;

  return $output;
}

If the user enters a value in the keywords field and clicks the Search button, I would like that value to appear in the keywords field in the submitted form on the resulting page. That should of course be done by passing an extra argument to drupal_get_form. But where can I obtain the value for this argument? After drupal_get_form redirects the page, $_POST is null. So what's the trick – or rather, what's the preferred Drupal technique? My examinations of other modules reveal obscure twiddles and comments along the lines of “I'm not sure why this works.”

Comments

AjK’s picture

Yes, probably one of the biggest FAQs. So much so a lesson was done on it here: http://drupaldojo.com/lesson/work-with-your-db-the-drupal-way

swartik’s picture

Andy,

Thanks for the pointer. I didn't want to save search requests in our database, but I noted how you returned a custom URL from your _submit function. I realized I could return a URL that includes a query. That is:

function my_search_form_submit($form_id, $form_values) {
  /* ... */
  return url($_REQUEST['q'], 'keywords='. urlencode($form_values['keywords']), null, true);
}

This solves my problem very nicely. I knew there had to be a Drupalphilic approach.

Steve