views:

119

answers:

2

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.

+1  A: 

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.

Mark Trapp
Thanks, I'll give a that ago
James Bayliss
A: 

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.

mongolito404
Yes my form is displayed at myurl. If the above doesn't work i'll look in to #redirect. Thanks.
James Bayliss
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.
mongolito404

related questions