Hi,

Please help me, I'm really stumped!

I'm writing a couple of modules for a site I maintain. The modules have forms which access something from the database. I want to then display the results of the query back to the user.

I'm struggling with the final step of getting my submit function to put data back onto the client screen. All I've managed to do so far is use drupal_set_message.

fyi, I've already read: http://drupal.org/node/204270 And I found a couple of forum topics on this, but I can't find the urls for them. None of which has helped.

Some solutions I've already tried:
- Numerous AJAX modules.. none of which seemed to allow me to do what I want, they seem to be more about dynamic forms - I just want to output lots of data from the database.
- #after_build, still don't really understand this, but all my attempts failed.
- the code below has my last attempt - to edit a markup element on the form from the submit function

I've done this before outside of drupal when I was using wordpress by writing a crude custom php/js set of scripts to handle the AJAXy stuff. And I've built vanilla php tools to do the same thing before I was using wordpress. Doing things the Drupal way, however, is currently stumping me.

Here's the code for the form:

function christchurchajax_test_form($form_state) {

  // ....
  // Some database stuff to populate $biblebooks and $speakers
  // ...

  $form['passage'] = array(
    '#type' => 'select',
    '#title' => t('Search by Bible Book'),
    '#options' => $biblebooks
  );
  $form['speaker'] = array(
    '#type' => 'select',
    '#title' => t('Search by Speaker'),
    '#options' => $speakers
  );
  $form['dates'] = array(
    '#type' => 'fieldset',
    '#title' => t('Search by date'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['dates']['after'] = array(
    '#type' => 'date',
    '#title' => t('Given After')
  );
  $form['dates']['before'] = array(
    '#type' => 'date',
    '#title' => t('Given Before')
  );
  $form['results'] = array(
  '#type' => 'markup',
  '#value' => ''
  );
  $form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
  $form['submit'] = array('#type' => 'submit', '#value' => t('Search'));
  return $form;
}

Then here's the code for submit:

function christchurchajax_test_form_submit($form, &$form_state) {
  $form['results']['value'] = '<p>Your search has been completed</p>';
  drupal_set_message(t('Your search has been completed.'));
}

Help gratefully received!

Comments

nevets’s picture

Any reason you are not using views and exposed filters?

reformedminister’s picture

I did look into views as one possible solution (I realise that I forgot to include it in my list of things I'd tried above).

I struggled to understand how it could help me. I'm looking at the docs again now. But any help on how to get started solving my would be appreciated!

kriskd’s picture

I'm subscribing because I'm working on my first module (just a little dumb thing) in an effort to learn all this stuff.

I've been looking at the form_submit function in existing contrib mods to try to get a handle on it. I don't think I've found one that returns data so I'm guessing that can not be done?

What I have going on in my form_submit is some data processing ending with an output string, but I have no idea what to do from there or how to display it. Basically:

function mymodule_form_submit($form, &$form_state) {
    // A bunch of processing
    $output = 'a string';
    return $output; 
}

So how do I handle my output or is that sort of processing not meant to take place here?

rajeshs.ind’s picture

If it's just s string that you want to be displayed, use drupal_set_message() function.

function mymodule_form_submit($form, &$form_state) {
// A bunch of processing
$output = 'a string';

drupal_set_message($output);

}

petebarnett’s picture

Hey reformedminister,

Check out this forum post that I answered... It sounds like it's just this basic setup you're describing
http://drupal.org/node/392712

Cheers,
Peter

jaypan’s picture

What do you mean by "put data back onto the client screen"? Do you mean you want to redirect them to a page with this data? Do you want to have a nice flashy javascript popup? Or a traditional javascript alert box? It's not really clear to me what you are trying to do.

Contact me to contract me for D7 -> D10/11 migrations.

enrique.delgado’s picture

Jay, I think he means, just outputting HTML on the screen. Imagine a form that generates some table based on the input fields. How would you go about that?