0

I have a simple form with 1 select and I want it after the form submit to redirect to the same URL, but with added GET params.


So in a form submit I added:

$form_state['redirect'] = url(arg(0).'/'.arg(1).'/'.arg(2), array(
  'query' => array(
      'brand' => $state['values']['brand'],
      'categ' => arg(2)
)));

Which redirects to wrong encoded url:

/produkty/vlasy%3Fbrand%3D118%26categ%3D27


But It works well when I use drupal goto (this is not recommended):

  drupal_goto(arg(0).'/'.arg(1).'/'.arg(2), array(
      'query' => array(
          'brand' => $state['values']['brand'],
          'categ' => arg(2)
  )));

/produkty/vlasy?brand=118&categ=27


Why is it so?

1

You just need to adjust the param value from a string to an array for $form_state['redirect']

It is in the documentation for drupal_redirect_form

$form_state['redirect'] determines where to redirect the user. This can be set either to a string (the path to redirect to), or an array of arguments for drupal_goto().

Example:

$form_state['redirect'] = array(
  arg(0) . '/' . arg(1) . '/' . arg(2),
  'query' => array(
        'brand' => $form_state['values']['brand'],
        'categ' => arg(2)
  )
);
| improve this answer | |
-3

Use urldecode() function.

$redirect_url = url(arg(0).'/'.arg(1).'/'.arg(2), array(
  'query' => array(
      'brand' => $state['values']['brand'],
      'categ' => arg(2)
)));
$form_state['redirect'] = urldecode($redirect_url);

Output: /produkty/vlasy?brand=118&categ=27

| improve this answer | |
  • This will still pass a string to $form_state['redirect'] which will then be urlencoded by drupal_goto – David Thomas May 3 '17 at 11:13

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.