Hey guys,

I am trying to do something that seems like it should be very simple but am running into issues.

I am wanting to use $form_state['redirect'] to send the user to a page of my module with the form values passed in the url. Again this seems like it should be really easy but I keep getting "page cannot be found" because drupal is encoding the url instead of passing the string as written.

Here is an example of my code:

function index_form_submit($form, &$form_state){

$search_string = $form_state['values']['index_search'];
$filter = $form_state['values']['filter'];

if($filter == 0){
    $form_state['redirect'] = 'page1&id=' . $form_state['values']['index_search'];
} else {
    $form_state['redirect'] = 'page2&id=' . $form_state['values']['index_search'];
}

I am wanting it to simply redirect to: /?q=page1&id=whatever

What I get is: /?q=page1%26id%3Dwhatever

The above comes up as page not found.

I have tried running urldecode on it but that doesn't help so I am kind of confused as to what is going on.

Any help is appreciated.
}

Comments

chrisdpucci’s picture

Nevermind.

It looks like using drupal_goto works perfectly for this so no need to use form_state['redirect']

Thanks anyway, hopefully this will help someone in the future.

lucas.7bd’s picture

Hello!

I'm running drupal 6.4,

When I use drupal_goto ("mdl.php?q=ania") all I get is mdl.php%3Fq%3Dania.

How to deal with this?

Best regards, Lucas.

paul_gregory’s picture

According to the API page for drupal_goto() (http://api.drupal.org/api/function/drupal_goto/6) the query string part of the url (the bit after the ?) goes in the second parameter of the function.

So mdl.php?q=ania becomes:

drupal_goto("mdl.php", "q=ania")

Paul

vkareh’s picture

A bit old, this thread, but the solution is the following:
$form_state['redirect'] = array('path', 'var1=value1', 'var2=value2', etc...);
The resulting redirection will look like: ?q=path&var1=value1&var2=value2...

So, in your case, it would be form_state['redirect'] = array('page1', 'id=' . $form_state['values']['index_search']);

petrica.martinescu’s picture

Yes, thanks for answering, it is wise to user form_state['redirect'] rather then drupal_goto() because by using drupal_goto function all the submit functions following your submit function will not get trigger.

A better and simpler approach of submitting all the values of the form through http query is this

$form_state['redirect'] = array($_GET['q'], $form_state['values'])

This will simply pass all your POST values to GET. You could of course unset form_build_id and form_token for a nicer URL result.

prolineserver’s picture

...and works only with one parameter. The correct way is actually
$form_state['redirect'] = array('path', array('var1'=>'value1', 'var2'=>'value2'), 'var3');
which results in path?var1=value1&var2=value2#var3.

petrica.martinescu’s picture

it's the same thing. $form_state['values'] is actually an array: variable => value.

Roze-1’s picture

Help needed !!

What if you get something like this in the url

-> $form['#redirect'] = array('user/'.$id.'&quicktabs_15=6#quicktabs-15');

And Output i get as -> ?q=user/32%26quicktabs_15%3D6%23quicktabs-15

Tried the urldecode but nothing is working up.

Thanks in advance

Roze-1’s picture

Putting the above in this method works !!

$form['#redirect'] = url('user/'.$id,array('query'=>'quicktabs_15=7#quicktabs-15','absolute' => TRUE));

nitishchopra’s picture

thanks vkareh it was very usefull

sirkitree’s picture

I just had to do this today and here is the solution i came up with: (d6)

1) hook_form_alter to add in a validation handler that runs before all others and extra submit handler

function mymodule_form_alter(&form, &$form_state, $form_id) {
  if ($form['#id'] == 'user-login' || $form['#id'] == 'user-login-form') {
    array_unshift($form['#validate'], 'mymodule_login_validate');
    $form['#submit'][] = 'mymodule_login_submit';
  }
}

2) validation handler that looks at the users table login field which is 0 if a user has never logged in

function mymodule_login_validate($form, &$form_state) {
  // rather intense, could do a single query here instead
  $user = user_load(array('name' => $form_state['values']['name']));

  if ($user->login == 0) {
    // set a designator so that we can redirect on submit
    $form_state['values']['redirect'] = 'user/'. $user->uid .'/edit';
  }
}

3) submit handler to overwrite the redirect key

function mymodule_login_submit($form, &$form_state) {
  if (isset($form_state['values']['redirect'])) {
    $form_state['redirect'] = array($form_state['values']['redirect']);
  }
}


~Professional: Lullabot
~Personal: jeradbitner.com

Scott Reynolds’s picture

I would avoid

$form['#id'] == 'user-login' || $form['#id'] == 'user-login-form')

and use

$form_id == 'user_login' || $form_id == 'user_login_form'

The id properties are assign to and given that #384992: drupal_html_id does not work correctly in AJAX context with multiple forms on page is now in D7 the #id will not be totally stable for this.

dan3h’s picture

Watch out if setting $form_state['redirect'] in a form submit handler. If $form['#redirect '] has been set, it will trump whatever you have set in $form_state['redirect'].

For confirmation of this, have a look at the code for drupal_redirect_form(), in includes/form.inc.

ranm5’s picture

I have this in one module:

$form_state['redirect'] = array('node/3',$it);
And in my node 3 it url appears as http://example.com/itc?10032
(itc is alias for node/3). My question is how can i get the value of 10032 in node/3 ?

m4olivei’s picture

In D7, the function signature of drupal_goto changed such that the second parameter is an $options array like that passed into the url() function. So to set $form_state['redirect'] in a D7 form submit function you would do something like:

$form_state['redirect'] = array('path/to/page', array('query' => array('param1' => 'value1', 'param2' => 'value2')));
manojpareta’s picture

Here is the code that work`s for me:

<?php
// $Id: My module.module
function My_module_form_alter(&$form, $form_state, $form_id) {
    if($form_id == 'search_form') {
        $form['buttons']['submit']['#submit'][] = 'My_module_node_form_submit';
    }
}

function My_module_node_form_submit($form, &$form_state) {
    unset($form_state['rebuild']);
    $form_state['redirect'] = array('your/custom/path');

}
?>
alinouman’s picture

Hello all i m also facing a form submitting problems. This is the data which iam posting to paypal and works fine but i m not able to validate the amount. Plz any idea how to validate the amount field that it should not be null before posting? and posting should also be done right.

 $forms=array();
   $forms['#action'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    $forms['amount']=array('#type'=>'textfield',
                          '#title'=>t('Enter Credit Amount'),'#description'=>t('Number of credits to purchase for user'),
                          '#required'=>TRUE,'#name'=>'amount',);
    
   
   //for paypal
     $forms['cmd'] = array(
    '#type'  => 'hidden',
    '#value' => '_xclick',
    '#name'  => 'cmd',
  );
   $forms['business'] = array(
    '#type'  => 'hidden',
    '#name'  => 'business',
    '#value' => 'anouma_1312968620_biz@gmail.com',
  );

  $forms['item_name'] = array(
    '#type'  => 'hidden',
    '#value' => 'Credits to Unlock',
    '#name'  => 'item_name',
  );
  $forms['rm'] = array(
    '#type'  => 'hidden',
    '#value' => '2',
    '#name'  => 'rm',
  );

  
   $forms['return'] = array(
    '#type'  => 'hidden',
    '#value' => preg_replace('/credit/','thanks',selfURL()),
    '#name'  => 'return',
  );
   $forms['notify_url'] = array(
    '#type'  => 'hidden',
    '#value' => preg_replace('/credit/','ipn.php',selfURL()),
    '#name'  => 'notify_url',
  );
   $forms['cancel_return'] = array(
    '#type'  => 'hidden',
    '#value' => selfURL(),
    '#name'  => 'cancel_return',
  );
   
   $forms['submit']=array('#type'=>'submit','#value'=>t('Pay Now through PayPal'),
                          '#prefix' => '<div class="paypal">',
                          '#suffix' => '</div>',); 
   return $forms;
Raphael Dürst’s picture

I have a also a problem with $form_state['redirect'].

My code looks like this:

function my_module_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'stormproject_node_form':
      // some custom form fields
      $form['#submit'][] = 'my_module_alter_submit';
      break;
  }
}

function my_module_alter_submit($form, &$form_state) {
  // some code
  $form_state['redirect'] = array('node/'. $team->nid .'/edit');
}

I tried several things, but nothing really works.
The form just doesn't redirect to the path I specified.
Does anyone have an idea who the redirect could work?

karol haltenberger’s picture

unset($_REQUEST['destination'], $_REQUEST['edit']['destination']);
http://drupal.org/node/942458#comment-3580862

Also $form['#redirect'] seems to override $form_state['redirect']