I'm trying to hook in to redirect on an ajax registration after the form is submitted. The form does not redirect, and the popup registration form stays put after a successful registration.

I want to redirect if the registration is successful. My code is below.

I'm trying to hook in to redirect on an ajax registration after the form is submitted. The form does not redirect, and the popup registration form stays put after a successful registration.

I want to redirect if the registration is successful. My code is below.

/**
* Implementation of hook_form_alter().
*/

function tester_ajax_form_alter(&$form, &$form_state, $form_id) {

 // user login form
     if ($form_id == 'user_login' || $form_id == 'user_register_form') {
      $form['actions']['submit']['#ajax'] = array(
        'callback' => 'tester_ajax_callback',      
        'wrapper' => str_replace('_','-',$form['#form_id']),
        'method' => 'replace',
        'effect' => 'fade',
      );
      $form['#submit'][] = 'tester_ajax_form_submit';
    }


  // see if webform_client_form_ is in the form_id
  if(strstr($form_id, 'webform_client_form_')) {
    // get the nid so we can use it in the wrapper value
    $nid = $form['#node']->nid;

    if ($nid > 4000) {
      // add the ajax properties to the submit button
      // we use 4000 because that is the cutoff between old and new forms - GTS
      $form['actions']['submit']['#ajax'] = array(
        'callback' => 'tester_ajax_callback',
        'wrapper' => str_replace('_','-',$form['#form_id']),
        'method' => 'replace',
        'effect' => 'fade',
      );
      $form['#submit'][] = 'tester_ajax_form_submit';
    }
  }
}

function tester_ajax_form_submit(&$form, &$form_state) {
  //You'll get an error about undefined index without this
  $form_state['rebuild'] = FALSE;
  if ($form['form_id']['#value'] == 'user_register_form') {$form_state['redirect'] = drupal_goto(drupal_get_destination());}
}
 
function tester_ajax_callback(&$form, &$form_state) {
  // define the $sid variable (submission id from webform)
   
  $sid = $form_state['values']['details']['sid'];
  // if we have a sid then we know the form was properly submitted, otherwise, we'll just return the existing $form array
  if ($sid) {
    // first we have to load up the webform node object
    $node = node_load($form_state['values']['details']['nid']);
    // create an array up with the confirmation message, retreived from the webform node
    $confirmation = array(
      '#type' => 'markup',
      '#markup' => check_markup($node->webform['confirmation'], $node->webform['confirmation_format'], '', TRUE),
    );
    // return the confirmation message
    return $confirmation;
  }
  else {
    // return the form
    return $form;
  }
}

Comments

vm’s picture

consider moving this thread to the Module development & code questions forum.

Usha Gavva’s picture

In the implementation of hook_form_alter,

<?php
/**
* Implementation of hook_form_alter().
*/

function tester_ajax_form_alter(&$form, &$form_state, $form_id) {
  global $base_url;
   if($form_id == 'user_register') {
     // .... write your code ...
     $form['#redirect']= $base_url."/node";
   }
}

/node - is the node to which you want to redirect after successful registration.

dsevcik’s picture

This did not work. It simply confirmed registration on the popup ajax register form and did not redirect.

I changed user_register to user_register_form since that is the form Id.

dsevcik’s picture

Solved from another thread:

function tester_ajax_form_submit(&$form, &$form_state) {
  //You'll get an error about undefined index without this
  $form_state['rebuild'] = FALSE;
  if ($form['form_id']['#value'] == 'user_register_form' || $form['form_id']['#value'] == 'user_login'){
     $form_state['redirect'] = 'home';
     ctools_include('ajax');
     ctools_add_js('ajax-responder');
     $commands[] = ctools_ajax_command_redirect(drupal_get_destination());
     print ajax_render($commands);
     exit;
  }
}