1

Using drupal 7 how can I make a selection alter where a page is then redirected to ? I need the value selected by a drop down menu to be part of the destination url for the redirect. For instance if I had the following for a drop down menu:

$form['changethis'] = array( '#type' => 'select', '#options' => array( 'one' => 'one', 'two' => 'two', 'three' => 'three', ),

Selecting option 'two'would send the page to www.mypage.com/two

Any help would be great I have experimented with an ajax request and hook_menu_alter , but no luck so far.

1 Answer 1

2

You can add your own submit handler to your form. In the submit handler function you will be able to access the value that was entered by the user.

The code could look something like this (to be put in a custom module).

    /**
    * Implementation of hook_form_alter()
    */
    function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
      if($form_id == 'MY_FORM') {
        // Adds our own custom submit handler to the form MY_FORM
        $form['#submit'][] = 'MYMODULE_MYFORM_custom_submit';
      }
    }

    /**
    * Custom submit handler for MY_FORM
    */
    function MYMODULE_MYFORM_custom_submit($form, &$form_state) {
      $form_state['redirect'] = 'my-path';
      $form_state['rebuild'] = TRUE;
    }

Replace 'my-path' with the value the user entered. Look inside $form_state['values'] to find it. Don't forget to check that the value is valid so no funky stuff is done here :)

Good luck.

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.