1

I have custom module, where after form_submit, i do some calculations, and get result of those calculations in a variable.

After i hit "submit", i can output those calculations with drupal_set_message , but i would like to format results (array), and print it bellow the submit button. How can i output resulting variable bellow the form or in the form, after submission ?

function mymodule_form_submit($form, &$form_state) {

$a=$b+$c;

// on submit, i would like to print/format $a on the reloaded form page

}

Thanks!

3 Answers 3

5

What you want to do is use something like $form_state['temporary'] in your myMod_submit function. Set $form_state['temporary'] = $someValue and rebuild your form. You also want your myMod_form function to have a place holder for your data. When the form is loaded, simply check to see if your temp holder is populated. if it is then you know you have a result and display it.

See code below.

function myMod_form($form, &$form_state) {

    ...

    if(!empty($form_state['temporary'])) {
        $form['results'] = array(
            '#type'     => 'item',
            '#markup'   => $form_state['temporary'], 
        );
    }

    return $form;
}

function myMod_submit($form_id, &$form_state) {
     ...

     $results = callSomeFunctionToGetResults();

     $form_state['temporary']   = $results;
     $form_state['rebuild']     = TRUE;

     ...
 }

i omitted all my code that would populated $results but this should get you to where you need to go.

quick addition here.

I just realized that you wanted it to go to another page and not the same page. I believe in your myMod_submit function you can use drupal_goto(). pass it a url and also pass it some options. you can then parse out the options as your values in the other page. See drupal_goto. however, i haven't tested this option out.

1
  • It seems that this is exactly what I need, and I wish also to stay on the same page, so your example will probably do the trick.Will check on desktop and accept answer . Tnx
    – NenadP
    Dec 13, 2012 at 16:55
0

If its a node form you can add an extra form submit handler to get the values Printed if not you probably have a submit function try print_r

As its a form submit you should try exit OR die

function my_module_my_form_submit($form, &$form_state) {
    //drupal_set_message(t('The form has been submitted.'));
    //drupal_set_message('<pre>'.print_r($form_state, 1).'</pre>');
    print_r($form_state, 1);
    die('Check the die !');
}

Basic form with submit handler

How do I add an additional submit handler

3
  • i can output the results this way, but i want to format them after submission, after page has been reloaded. So calculations done after form submit must be "remembered" and outputed in "next screeen". It is in fact module that operates in admin area.
    – NenadP
    Dec 11, 2012 at 13:02
  • I dont want to use drupal_set_message, rather to get calculation results and show it on the reloaded submission page
    – NenadP
    Dec 11, 2012 at 13:29
  • @HoverFusion see my edit.
    – niksmac
    Dec 11, 2012 at 13:39
0

A simple solution to access variables outside the function in other function is to make the scope of the variable as global.

function mymodule_form_submit($form, &$form_state) {
  global $a;
  $a=$b+$c;    
}

And on submit, in other function you can access this variable by something like:

function some_page_load_function() {
  global $a;
  //print/format $a according to requirement.
}

I've written the function name to be some_page_load_function, as I'm not sure about the function you're wishing to access the value; as you also mentioned that this module might be a part of admin region.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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