I want to use AJAX to submit a form & replace some content while the code runs in the background. I've made a form that works but if I ad the AJAX callback it no longer actions the form.

This may be really obvious but I can't seem to get it working, is there a trick to it?

Here is a simplified version of what I'm trying to do. Any feedback is appreciated.

function trial_registration_form($form, &$form_state) {

  $form = array();
  $form['#method'] = 'post';
  $form['#action'] = url(register.php');
  $form['#suffix'] = '<div id="form-register-message"></div>';

  $form['name'] = array(
      '#title' => t('Name'),
      '#type' => 'textfield',
      '#required' => TRUE,
    // submit
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
      '#ajax' => array(
        'callback' => 'trial_registration_form_submit',
        'wrapper' => 'form-register-message'
      '#submit' => array('trial_registration_form_submit'),
    return $form;

function trial_registration_form_submit($form, &$form_state) {

  // create message
  $content = t('Thank you for registering. An email with login details will be sent to you shortly.');

  // Process form submission.
  $commands = array(
    ajax_command_replace('#form-register-wrapper', $content),

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

  return $replace;


kirantej_p’s picture


I have a similar requirement. Please post back here if you get the answer for the above query posted.


kirantej_p’s picture

Try this:

function trial_registration_form_submit($form, &$form_state) {
   $element = $form;
   if(!form_get_errors()) {
       $element = t('Thanks for registering. We will contact you soon');
   return $element;

This works for the ajax submit. This will replace the form with our custom message.

Jaypan’s picture

Changing the #action of the link will cause the ajax to fail. Why are you changing the action? You should almost never have to do this in Drupal.

Currently looking for employment in Canada, USA or Asia. My portfolio is at Jaypan, and you can checkout my Japan podcasts.