1

I'm trying to wrap my head around the process of creating a form and handling the submitted data using x_submit().

I start by having a form callback:

function world_form(&$form_state) {

  // if ($form_state['post']) { drupal_set_message("Solved!"); }

  $forms['fieldset_options'] =
    array('#type' => 'fieldset',
          '#title' => t('Options'),
          '#collapsible' => FALSE,
          '#collapsed' => FALSE);

  $forms['fieldset_options']['tags'] =
    array('#type' => 'item', '#title' => 'Simple stuff');

  $forms['fieldset_options']['checkbox1'] =
    array('#type' => 'checkbox', '#title' => t('Solve world problems'));

  $forms['fieldset_options']['submit'] =
    array('#type' => 'button', '#button_type' => 'submit', '#value' => 'Go!');

  // $forms['submit'] = array('#type' => 'submit', '#value' => 'Go!');

  return $forms;
}

So I specify a theme callback, which takes me to a file world.tpl.php in which I do:

print drupal_get_form('world_form');

which invokes my form callback. So far so good. Now the problem I have is that I want to handle when this form is submitted (more specifically, I want the value of the checkbox when it is checked). I read a lot of documentation and most mention the world_form_submit function. So I create one like this:

function world_form_submit($form, &$form_state) {
  drupal_set_message("Posted!");
}

But after I've done that the function isn't called. However if I instead use the line which is commented out in my form function and comment out the '#type' => 'button' field it DOES get called and I can now access the checkbox state, however the problem I have now is that the form is not updated with the state of the checkbox (I.e. if I check the box and submit I get to the same page but the box is no longer checked).

So my questions are:

  1. Why isn't the _submit callback called when I use a "regular" submit button? Do I have to use the $forms['submit'] way?
  2. If I do get the _submit callback called, can I still update my form to reflect what it was before I submitted, and
  3. How can I get the checkbox state in my world.tpl.php file? Say I want to print something if it is checked.

Thank you for your time.

1 Answer 1

1

Many questions here:

  • Your form function function should return the form array and doesn't need an argument unless you need it for something specific.
  • You should at least have an element of type 'submit' (not button) in your form.
  • Chances are good your submit function will get called if you implement it this way.
  • If the form fails validation, FAPI will restore it to the values submitted, keeping what was typed by the user.
  • You shoudn't try to embed business logic in your template, but rather in your module and/or a preprocess function.

2
  • Sorry I forgot to add the return statement in the example. I added it now. It is there. But the problem for me now is still that I want to do something with my checkbox values... assuming I do something in my _submit function, how would I return the result to the template file? E.g. I fetch data from a db in the _submit function, where do I store that data to have it printed out in the template?
    – sPirc
    Mar 1, 2012 at 11:44
  • Ideally, with a theme function. This document may be of help.
    – Countzero
    Mar 1, 2012 at 12:20

Your Answer

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

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