2

I have one form that is just an options input. When I select a value then go to the next page there is another form that I want to have three hidden values based upon the chose value of the first form.

With Form API, what is the best way to pass the value from the first form to the next and have three values from the second form be determined by the single value chosen from the first form?

  • 7
    Normally you'd combine them both into a single multi-step form so the state is retained. I'd see if that's what you're looking for first, but if you can't use that method there are other ways so just update the question :) – Clive Jun 11 '13 at 18:48
  • @Clive will do, thanks for the point in the right direction – CR47 Jun 11 '13 at 18:49
  • @Clive I've created another question which is somewhat of an update of this one. drupal.stackexchange.com/questions/76550/… – CR47 Jun 17 '13 at 12:43
1

Though multi-step form is your best choice, you can do it with two separate forms as well, using bit of trickery.

Say you have form_id_1 on your path1 URL. And another form_id_2 on your path2.

If you want pass values from the first form to the second, you have 2 options:
1) encode your values in form page path, like path2/arg1/arg2/arg3. Obvious downside is that arguments are visible to the user.
2) hijack Batch<->Form workflow. In form_id_1 submit handler use following code:

$new_form_state = array();
// list path arguments that usually go into form_id_2 constructor from path2
$new_form_state['build_info']['args'] = array();
$new_form_state['method'] = 'post';
// list values you want to pass.
$new_form_state['values'] = array();
$_SESSION['batch_form_state'] = $new_form_state;
drupal_goto("path2");

User will be redirected to path2 and when form_id_2 is built for that page it will have your values in its $form_state.

1

You can do a multistep form, there is a clear usage example in the Examples module. Here is an excerpt about it from the module's project page:

The Examples for Developers project aims to provide high-quality, well-documented API examples for a broad range of Drupal core functionality.

The Examples project contains many modules which hopefully illustrate best practices for implementing various Drupal APIs. These modules can be enabled individually, and will typically add menu items to your site, which should guide you through their features. You can then look through the code to see what they are doing.

Developers can learn how to use a particular API quickly by experimenting with the examples, and adapt them for their own use.

Note also that these modules demonstrate internals for Drupal development in PHP (and some JavaScript). Not all Drupal users will need these examples. There are many contributed modules which will do the majority of what a Drupal site-builder could need. Be sure and explore other contributed modules to see if you can find one that meets your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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