Drupal 7 redirect to a specific page (a tab) after form submit

14,328

You've forgotten to pass $form_state by reference, so your changes work only in the form_alter function.

Change your function signature as follows. Note &$form_state:

function ulsform_form_alter(&$form, &$form_state, $form_id) {

$form_state['redirect'] could be either an array or a simple string. if it's a string, user will be redirected there. If it's an array, it will work according to how drupal_goto works.

$form_state['redirect'] = array(
  'node/123',
  array(
    'query' => array(
      'foo' => 'bar',
    ),'fragment' => 'baz');

To node/123?foo=bar#baz

$form_state['redirect'] = 'node/123'

to node/123

Share:
14,328
Mike Boutin
Author by

Mike Boutin

Learning is for everyone. No questions is stupid or not deep enough, we're all at different levels. Be kind.

Updated on June 04, 2022

Comments

  • Mike Boutin
    Mike Boutin almost 2 years

    I'm trying to redirect the page after i submited the form. The form is at the page 'formulaires/demande-de-subvention-pour-les-entraineurs' and i want to redirect to 'formulaires/demande-de-subvention-pour-les-entraineurs/entraineurs' How can i do this?

    function ulsform_form_alter(&$form, $form_state, $form_id) {
      if ($form_id == 'ulsform_demande_de_subvention_pour_les_entraineurs_form') {
        $form['#submit'][] = 'ulsform_demande_submited_form';
      }else if($form_id == 'ulsform_demande_de_subvention_pour_les_entraineurs_02_form'){
        $form['#submit'][] = 'ulsform_entraineur_submited_form';
      }
    }
    
    function ulsform_demande_submited_form(&$form, $form_state){
      global $user;
    
      $v = $form_state['values'];
      $form_state['redirect'] = 'formulaires/demande-de-subvention-pour-les-entraineurs/entraineurs';
    
    /* My code to insert into the db */    
    
    
    }
    

    It doesn't work and when i try to create a form['#redirect'] like a saw on another post, it doesn't work too. What can i try?

    Thank you for your help! I'm new to drupal