I cannot for the life of me figure out why my ctools_ajax_command_redirect is not working,
I had it working properly in the past but now it just does absolutely nothing (ctools_ajax_command_reload() is also broken).

Below is my ajax callback function for a form and what I want to happen is that on a successful form submission to redirect to the new node, and on a form with errors to display the errors and not allow a submission without refreshing page.

function node_create_callback(&$form, &$form_state)
{

    module_load_include('inc', 'node', 'node.pages');
    node_form_validate($form, $form_state);
    if (!form_get_errors())
    {
        node_form_submit($form,$form_state);
        ctools_include('ajax');
        ctools_add_js('ajax-responder');
        $commands[] = ctools_ajax_command_redirect("node/ ");
        return array('#type' => 'ajax',  '#commands' => $commands);
    }
    else
    {
        $commands = array('#type' => 'ajax', '#error' => FALSE, '#commands' => array());
        $output = drupal_render($form);
        $commands['#commands'][] = ajax_command_insert(NULL, $output);
        // Prepend status messages to the node form.
        $commands['#commands'][] = ajax_command_prepend(NULL, theme('status_messages'));
        return $commands;
    }
}

Comments

tce’s picture

instead of

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

I would try

<?php
print ajax_render($commands);
drupal_exit();
?>
jlava’s picture

I am doing this and it doesn't works. Can anyone help me?

$url = 'http://www.drupal.org';  
ctools_include('ajax');
ctools_add_js('ajax-responder');

$commands[] =  ctools_ajax_command_redirect($url);
print ajax_render($commands);
drupal_exit();
die();
Jaypan’s picture

You probably need this:

  $url = 'http://www.drupal.org';
  ctools_include('ajax');
  ctools_add_js('ajax-responder');
  $commands[] =  ctools_ajax_command_redirect($url);

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

I say 'probably' because I do it myself and don't use ctools. But the above is how to return ajax commands.

gurunathan’s picture

I am also facing the same. issue? any solution?

phponwebsites’s picture

I've tried this code

$path = 'http://localhost/newpage';
 ctools_include('ajax');
  ctools_add_js('ajax-responder');
  $commands[] = ctools_ajax_command_redirect($path);
  print ajax_render($commands);
  exit;

It is working for me. But i want to open in new tab. how can i do this?

zorax’s picture

Hello,
I have a similar problem
I need to redirect dynamically width query:

$path = 'accueil?localite=;'.$form_state['values']['field_localite']['und'][0]['target_id'];
$commands[] = ctools_ajax_command_redirect($path);

here is the $path displayed = accueil%26%2363%3Blocalite%26%2361%3B24148
instead of = accueil?localite=24148

Jaypan’s picture

You probably need this:

$path = 'accueil';
$commands[] = ctools_ajax_command_redirect($path, array('query' => array('localite' => $form_state['values']['field_localite']['und'][0]['target_id'])));

I'm speculating though, as I don't use ctools.

mahimajulka’s picture

callback in modal_forms.pages.inc

function modal_forms_login($js = NULL) {
  // Fall back if $js is not set.
  if (!$js) {
    return drupal_get_form('user_login');
  }

  ctools_include('modal');
  ctools_include('ajax');
  $form_state = array(
    // 'title' => t('Log in'),
    'ajax' => TRUE,
  );

  $output = ctools_modal_form_wrapper('user_login', $form_state);
  if (!empty($form_state['executed'])) {
  
    // We'll just overwrite the form output if it was successful.
    $output = array();
    ctools_add_js('ajax-responder');
    $output[] = ctools_modal_command_dismiss(t('Login success'));
    if (isset($_GET['current'])) {
      $output[] = ctools_ajax_command_redirect($_GET['current']);
    }    
    elseif (isset($_GET['destination'])) {
      $output[] = ctools_ajax_command_redirect($_GET['destination']);
    }
    elseif(module_exists('login_destination')) {
      $destination = login_destination_get_destination('login');
      $output[] = ctools_ajax_command_redirect($destination['path']);
    }
    else {
      $output[] = ctools_ajax_command_reload();
    }
      
  }

Trailing Part of response as seen in browser net tab

......
{"command":"modal_dismiss"},{"command":"redirect","url":"\/sendflower\/bouquet_type
\/combos","delay":0}]

The modal form is closed but nothing happens. If I refresh the page user is shown as logged in

This same code works on my local system where the drupal installation is in sub-directory. But does not work on LIVE server where Drupal installation is on root directory

Shashwat Purav’s picture

+1

Thank You,
Shashwat Purav