3

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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
James Bayliss
  • 107
  • 1
  • 9
  • There is normally no reason to set `$form['#action']`; I have never changed it in all the forms I created. – apaderno Jul 27 '10 at 22:41

2 Answers2

8

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.

0

Is your form displayed on the page at myurl ? In order for a form submission to be processed, the form as to be displayed (using drupal_get_form()) on the page used as action.

You may also try to se the form #redirect to the landing page URL instead of its #action. This way, the form is submitted to its generating URL but the user is redirected to your destination page after processing.

Pierre Buyle
  • 4,883
  • 2
  • 32
  • 31
  • Yes my form is displayed at myurl. If the above doesn't work i'll look in to #redirect. Thanks. – James Bayliss Jul 27 '10 at 08:27
  • By displayed, I meant outputed by the handler for the myurl path. If the form markup is fetched from another path and then using AJAX and then added to the page, it will not work since when processing the form submission, drupal_get_form() will no be called. – Pierre Buyle Jul 27 '10 at 12:41