2

I just found out that when I try to submit a form with AJAX, in that instance user_login, the redirection doesn't work anymore. Is this normal? How can I make the redirection happen after the AJAX request comes back?

1
  • 4
    AJAX exists to avoid redirects in the first place. If you want redirects anyway, then there is no reason to use AJAX. Just submit the form without.
    – Berdir
    Jun 24, 2011 at 9:23

4 Answers 4

2

Like it has been said, there isn't really a reason to do a redirect and AJAX. If you however really want to do it, it is pretty simple.

Using jQuery it would look like this:

$.post(Drupal.settings.basePath + 'url/to/page', {post: 'params'}, function (data) {
  window.location = Drupal.settings.basePath + 'url/to/redirect/to';
});

The gist of it, is to set window.location to the url you want to redirect to, if you want to refresh the page you could do

 window.location = document.URL;
1
  • I have also been looking for this solution for days. Ajax prevents redirection of the form. For example I have a multistep wizard which uses ajax and the Form API and when the user completes I need do a redirect on the final submit to a ecommerce module page. Other than using the client-side jQuery above how else can I do this?
    – trampjuice
    Sep 3, 2011 at 11:42
3

I had the same situation: ajax login/register form until the fields are correctly filled, and then redirect the user to the logged in section. This is what I used, and it works fine for me, in the ajax callback function:

ctools_include('ajax');
ctools_add_js('ajax-responder');

$commands[] = ctools_ajax_command_redirect('<front>');

return array('#type' => 'ajax', '#commands' => $commands);

the full function looks like this:

function ajax_signup_detailed_submit_callback($form, &$form_state)
{
  $commands = array();
  $messages = drupal_get_messages(NULL, FALSE);

  if (count($messages))
  {
    $commands[] = ajax_command_replace("#better-messages-wrapper", theme('status_messages'));
  }
  else
  {
    ctools_include('ajax');
    ctools_add_js('ajax-responder');
    $commands[] = ctools_ajax_command_redirect('<front>');
  }

  return array('#type' => 'ajax', '#commands' => $commands);
}

Hope this helps, Istvan

1
  • I found this only works if you change drupal_get_messages to only get error messages by changing NULL to 'error'
    – Teegan
    May 4, 2012 at 8:11
1

The purpose of AJAX is to submit the form in background, and update the content of the form without refreshing the full page. If a redirection would happen, then the page would be refreshed, and replaced with the content returned from the server, which is the opposite of what AJAX tries to do.

AJAX is used to update part of a page without to refresh the full page. A background request is made to the server, and the value returned is used to update part of the page.
For example, AJAX is used to update a list of items, or to updated the value shown by a form field basing on the value selected from another form field. Suppose you have a drop-down list that reports the list of USA states, and another drop-down list that lists all the counties for that state, AJAX would be used to update the list of the counties when the user changes the state from, for example, Iowa to New York.

1
  • 1
    I agree although in my instance it s a login in form, so i used ajax to avoid reloading the page in case of a wrong password or user id, although in case of success then the user page is loaded. Is there anyway to do so ?
    – silkAdmin
    Jun 24, 2011 at 17:53
0

I found that the way to do what silkAdmin wants to do is not to use Ajax at all. If your submit handler determines that the password is invalid, set $form_state['resubmit'] to TRUE, and add a test in your form builder function that changes the text. This does reload the entire page, but there doesn't seem to be a way to avoid that.

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.