hook_form_submit not being called

James Bayliss picture James Bayliss · Jul 26, 2010 · Viewed 8.2k times · Source

I'm trying to submit a form and use hook_form_submit.

The problem is the form is displayed via ajax and this results in hook_form_submit not being called.

$items['ajaxgetform/%'] = array(  
  'page callback' => 'ajaxgetform',  
  'access arguments' => array('access content'),  
  'type' => MENU_CALLBACK  
);   

function ajaxgetform($form_id) {    
  drupal_get_form($form_id);  
  return drupal_json($panel);  
}  

function_myform_form($form_state) {  
  $form['myform'] = array(  
    '#title' => 'myform value',  
    '#type' => 'textfield',  
    '#default_value' => 'myform default value'  
  );  

  $form['#action'] = url('myurl');

  $form['submit'] = array(  
    '#type' => 'submit',  
    '#value' => 'submit'  
  );

  $form['#ajaxsubmit'] = TRUE;  
    return $form;  
  }  

hook_form_alter() does get called.

Below doesn't get called?

function myform_form_submit($form, $form_state) {   
  // ...  
} 

I'm not sure if this is a common problem, but i've been stuck for hours trying to make it work.

If I remove $form['#action'] = url('myurl'); myform_form_submit() gets called. However I get a white screen with jason script.

Answer

user113292 picture user113292 · Jul 27, 2010

There is no hook_form_submit(). Instead, you register submit handlers with $form['#submit']. So, if you want to call myform_form_submit() when the form gets submitted, add:

$form['#submit'][] = 'myform_form_submit';

to myform_form(). Take a look at the 5.x to 6.x form changes and the Forms API reference for more info.