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?
4 Answers
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;
-
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? Sep 3, 2011 at 11:42
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
-
I found this only works if you change drupal_get_messages to only get error messages by changing NULL to 'error'– TeeganMay 4, 2012 at 8:11
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.
-
1I 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 ? Jun 24, 2011 at 17:53
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.