Drupal sending a query string with $form_state[‘redirect’] FAPI

Sending a query string when redirecting a user using Drupal forms api

To redirect a user to a new page when the form is submitted, you use the submit function like so.

<?php

function your_module_form_submit($form, &amp;$form_state){
//on submit code goes here...........</code>

/// redirect user
$form_state['redirect'] = 'about';
}

?>

To send a url query along with this the obvious logical step would be to do the following

<?php

//cannot add a query string like this
$form_state['redirect'] = 'about?org=1&amp;fid=2';

?>

Unfortunately this does not work (feel free to try).
It is not well documented but you can pass a query string to $form_state[‘redirect’] in a similar way that you pass it to drupal_go_to() by assigning it an array. So our failed attempt above will now look like this.

<?php

$form_state['redirect'] = array('about', 'org=1&amp;fid=2'');

?>

note: you do not need to use the ‘?’ when writing your query drupal adds this in automatically.

Advertisements

3 thoughts on “Drupal sending a query string with $form_state[‘redirect’] FAPI

  1. In drupal 7 the correct way is:

    $form_state[‘redirect’] = array(‘about’, array(‘query’ => array(‘org’ => 1, ‘fid’ => 2)));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s