Hi everybody,

I am writing a module and a part of it is a form that allows the user to form a query: He or she can choose from some pop-ups the information she wants to view, and on submitting the form, she is going to see the result of the query AND the form again.

How can I achieve something like this with the Drupal-4.7 Forms-API?

What I do is the following: In my module 'test', I register a menu-item with a callback:

function test_menu($may_cache) {
  //...
  $items[] = array('path' => 'test/stats/jobs',
    'title' => t('Queries'),
    'callback' => 'test_stats_jobs_form',
    'access' => user_access('query statistics'),
    'type' => MENU_NORMAL_ITEM);
  //...
}

The function test_stats_jobs_form() builds the form and renders it via a 'return drupal_get_form(...)' statement. The submission is handled in a function test_stats_jobs_form_submit(). So far no problems.

However, how can I tell drupal to display the result of the query in front of the form itself, once the user submitted the form? The only output I can generate is a text message that I bring up using drupal_set_message()!

I would be very glad to receive a hint, I'm completely lost here and could not find any relevant information on the web!

Thanks,
Kaspar

Comments

hbfkf’s picture

I see that my question has been asked before (see "I know that" on http://drupal.org/node/82426) but not answered.

Anybody can provide a hint? Would be very heldful!
Kaspar

hbfkf’s picture

Answering myself, the following form definition code makes a form that outputs something before the form itself on submission:

function test_stats_jobs_form()
{
  // ...
  $form['submit'] =
    array(
          '#type' => 'submit',
          '#value' => t('Submit')
          );
  $form['#after_build'] = array('test_stats_jobs_form_after_build');
  $form['#redirect'] = FALSE;
  return drupal_get_form('test_stats_jobs_form', $form);
}

It uses the #after_build feature to embed the output:

function test_stats_jobs_form_after_build($form)
{
  global $form_values;
  $op = isset($_POST['op']) ? $_POST['op'] : '';

  if ($op == t('Submit')) {
    drupal_validate_form($form['form_id']['#value'], $form);
    if (!form_get_errors()) {
      $form['query_result'] =
        array(
              '#value' => 'whatever',
              '#weight' => -100
              );
    }
  }
  return $form;
}

This also partially answers http://drupal.org/node/82426.

dmnd’s picture

when I pass the $form back for the second call, and do a var dump on $form, $form is empty.

I guess this is because

function test_stats_jobs_form()

is defined with no argument.

I am calling the function from menu. How do I do a menu callback and pass an argument?

I looked at these 2 pages but didn't see a way
http://api.drupal.org/api/group/menu/4.7
http://api.drupal.org/api/constant/MENU_CALLBACK/4.7